views:

194

answers:

3

Hello everybody,

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 Commandbutton 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

A: 

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

John at CashCommons
See my comment below. I still don't know in what event should I check that property...
Edwin
A: 

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).

Marc Gravell
But what event should I use to get the CurrentRow? also, I forgot to mention, there are 2 buttons on the datagrid, how do I know if the clicked button was "edit" or "delete"?
Edwin
By hooking different buttons to different event-handlers... you should generally only re-use the handler if the intent is the same.
Marc Gravell
I'm using databound, meaning I don't know how many rows my grid is going to have. Can you give me a pointer on how to hook events when the grid data is obtained via .DataSource?
Edwin
A: 

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 gird, 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

David Hall
Thanks for the info. Now I feel that it was kind of a silly question from my part. This helps considerably, thanks!
Edwin
You're welcome - not a silly question either. I've come from the other direction myself, moving from the winforms world to webforms and had a lot of the same problems but in reverse. Glad I could help.
David Hall