开发者

Finding the click event of DGV button column and transferring to another form

I have a datagrid view with four columns:

productid 
productname
productprice
buy (this is button co开发者_开发知识库lumn )

Is it possible to find the click event of button column? I mean, if I click on the row 1 button, the corresponding row values will be transferred to another form.

If I click on the row 2 button, the corresponding values are transferred to another form. I am doing WinForms applications. Any ideas or sample code would be appreciated.


This is answered on the MSDN page for the DataGridViewButtonColumn.

You need to handle either the CellClick or CellContentClick events of the DataGridView.

To attach the handler:

dataGridView1.CellClick += new DataGridViewCellEventHandler(dataGridView1_CellClick);

And the code in the method that handles the 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);
    }
}


using validate cell you can get the (column & row of the cell). Initialize the button as win forms button object, also add a handler that call the click event of the same.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜