开发者

Windows DataGridView _RowCommand

My background is pretty much ASP.Net, and I was asked to develop a small windows application. I tried to use a grid to present and select data, and I tough that the equivalent in windows forms to the ASP.Net's GridView was DataGridView. I'm not sure yet if that is the case, basically, in ASP.Net, you have the _RowCommand event associated to the grid, that is triggered after a Comma开发者_C百科ndbutton is clicked. I noticed also that there is no such thing as a DataKeyNames property, so I don't know how to pass the current row key to the button clicked. Any help will be appreciated, thanks!

I forgot to mention: My grid has two DataGridViewButton type of columns, And I don't know the event that I need to code on to perform the selected command


The event you are looking for is the CellClick event - with the DataGridViewButtonColumn you do not actually associate event handlers to the particular buttons as you would with other buttons on your form.

From the event arguments returned by the cell click event you can then work out which row and column the click was in and from that what action you want to take.

private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{        
    DataGridViewButton cell = (DataGridViewButtonCell)
        dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];

    // Any additional logic you want
}

Beyond that however, I think you would really benefit from taking a step back and thinking about the differences between the winforms and webforms coding paradigms.

With webforms a lot of the way you code is dictated by the fact that everything is stateless. This is not the case with winforms, you have state and you can access most controls when you need to find out information about them.

For this reason, retrieving information like the currently selected cell in a DataGridView is trivial with winforms.

Also, in most cases with winforms you do not need specific buttons to edit or delete - you can edit directly in the grid, and use in inbuilt delete functionality (select the row and press the delete key).

A good place the get you past some of the bumps might be the DataGridView FAQ. It's an amazing resource for learing about the DataGridView


DataGridView.CurrentRow gets the selected row. Is that what you need?


If you are databound, you should have (via .CurrentRow) access to all the properties, by casting .CurrentRow.DataBoundItem to whatever type you need. Otherwise, just look at the cells, or set a .Tag against the rows when you add them.


Here's an example showing a data-bound DataGridView and a couple of buttons, pulling out data for the selected row:

using System;
using System.ComponentModel;
using System.Windows.Forms;
class Person
{
    public string Name { get; set; }
    [DisplayName("Eye Color")]
    public string EyeColor { get; set; }
}
static class Program
{   
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        using (var form = new Form())
        using (var grid = new DataGridView { Dock = DockStyle.Fill})
        using (var btn1 = new Button { Dock = DockStyle.Bottom, Text = "Button 1"})
        using (var btn2 = new Button { Dock = DockStyle.Bottom, Text = "Button 2" })
        {

            btn1.Click += delegate
            {
                form.Text = "Button 1 clicked";
                if (grid.CurrentRow != null)
                {
                    form.Text += ": " + ((Person)grid.CurrentRow.DataBoundItem).Name;
                }
            };
            btn2.Click += delegate
            {
                form.Text = "Button 2 clicked";
                if (grid.CurrentRow != null)
                {
                    form.Text += ": " + ((Person)grid.CurrentRow.DataBoundItem).Name;
                }
            };
            form.Controls.Add(btn1);
            form.Controls.Add(btn2);
            form.Controls.Add(grid);
            var data = new BindingList<Person>
            {
                new Person { Name = "Fred", EyeColor = "green"},
                new Person { Name = "Barney", EyeColor = "brown"},
                new Person { Name = "Wilma", EyeColor = "blue"},
                new Person { Name = "Betty", EyeColor = "blue"},
            };
            grid.DataSource = data;
            Application.Run(form);
        }
    }
}

There are other ways of handling the click events, and usually much of the above would be done via the designer rather than like this (but it is very hard to show designer code in a snippet).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜