C# 查找DGV按钮列的单击事件并传输到另一个窗体

C# 查找DGV按钮列的单击事件并传输到另一个窗体,c#,winforms,datagridview,click,buttonclick,C#,Winforms,Datagridview,Click,Buttonclick,我有一个包含四列的datagrid视图: productid productname productprice buy (this is button column ) 是否可以找到按钮列的单击事件?我的意思是,如果我点击第1行按钮,相应的行值将被转移到另一个表单 如果我单击第2行按钮,相应的值将传输到另一个表单。我正在做WinForms应用程序。任何想法或示例代码都将不胜感激。使用validate cell,您可以获得(单元格的列和行)。将按钮初始化为win forms button对象,

我有一个包含四列的datagrid视图:

productid 
productname
productprice
buy (this is button column )
是否可以找到按钮列的单击事件?我的意思是,如果我点击第1行按钮,相应的行值将被转移到另一个表单


如果我单击第2行按钮,相应的值将传输到另一个表单。我正在做WinForms应用程序。任何想法或示例代码都将不胜感激。

使用validate cell,您可以获得(单元格的列和行)。将按钮初始化为win forms button对象,并添加一个调用相同按钮的click事件的处理程序。

此问题在MSDN页面上的解决方案中得到了回答

您需要处理DataGridView的CellClick或CellContentClick事件

要附加处理程序,请执行以下操作:

dataGridView1.CellClick += new DataGridViewCellEventHandler(dataGridView1_CellClick);
以及处理evend的方法中的代码

void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
    // Check that the button column was clicked
    if (dataGridView1.Columns[e.ColumnIndex].Name == "MyButtonColumn")
    {
        // Here you call your method that deals with the row values
        // you can use e.RowIndex to find the row

        // I also use the row's databounditem property to get the bound
        // object from the DataGridView's datasource - this only works with
        // a datasource for the control but 99% of the time you should use 
        // a datasource with this control
        object item = dataGridView1.Rows[e.RowIndex].DataBoundItem;

        // I'm also just leaving item as type object but since you control the form
        // you can usually safely cast to a specific object here.
        YourMethod(item);
    }
}

我已经为单元格点击事件处理程序实现了另一个功能。有没有其他方法可以替代这个功能?你能建议我如何获得所有的值吗row@user852714您可以在该事件处理程序中实现任意数量的功能,特别是因为每个元素通常只作用于特定行的单元格。对于获取特定行中的值,最好的方法是我建议使用该数据绑定项的方法。否则,您只需迭代行单元格,即可检索每个单元格值。