找到DGV按钮栏的点击事件并转移到另一个表单

发布于 2024-12-02 01:19:31 字数 242 浏览 0 评论 0原文

我有一个包含四列的数据网格视图:

productid 
productname
productprice
buy (this is button column )

是否可以找到按钮列的单击事件?我的意思是,如果我单击第 1 行按钮,相应的行值将转移到另一个表单。

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

I have a datagrid view with four columns:

productid 
productname
productprice
buy (this is button column )

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

月下凄凉 2024-12-09 01:19:31

DataGridViewButtonColumn

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

附加处理程序:

dataGridView1.CellClick += new DataGridViewCellEventHandler(dataGridView1_CellClick);

以及处理事件的方法中的代码

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

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);
    }
}
不乱于心 2024-12-09 01:19:31

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文