Windows DataGridView _RowCommand

My background is quite a lot of ASP.Net, and I was asked to develop a small application for Windows. I tried using a grid to represent and select data, and I firmly argued that the window equivalent for ASP.Net GridView was a DataGridView. I'm not sure if this is the case, mainly in ASP.Net, you have a _RowCommand grid-related event that fires after you press the Commandbutton button. I also noticed that there is no such property as the DataKeyNames property, so I do not know how to pass the current row key to the button pressed. Any help would be appreciated, thanks!

I forgot to mention: my grid has two columns of type DataGridViewButton, and I don’t know what event I need to execute to execute the selected command

+3
source share
3 answers

The event you are looking for is an event CellClick- with the help of this DataGridViewButtonColumnyou do not actually associate event handlers with specific buttons in the same way as with other buttons in your form.

From the event arguments returned by the cell click event, you can then determine which row and column were clicked, and what you want to do.

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

    // Any additional logic you want
}

Also, however, I think that you can really take a step back and think about the differences between the coding paradigms of winforms and webforms.

- , , , . winforms, , , .

, DataGridView, winforms.

, winforms - ( ).

FAQ DataGridView. DataGridView

0

DataGridView.CurrentRow . , ?

0

, ( .CurrentRow) , .CurrentRow.DataBoundItem , . .Tag .


, DataGridView , :

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);
        }
    }
}

, , ( ).

0

Source: https://habr.com/ru/post/1725223/


All Articles