datagridview按钮列Winforms [英] datagridview button column Winforms

查看:215
本文介绍了datagridview按钮列Winforms的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 DataGridView 上有一个按钮列,我试图处理一个 Button.Click 发生在我点击按钮。任何帮助?

I have a button column on a DataGridView and I am trying to handle a Button.Click event but nothing happens when I click a button. Any help?

private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
    MessageBox.Show(e.ColumnIndex.ToString());
    if (e.ColumnIndex == 5)
    {
        MessageBox.Show((e.RowIndex + 1) + "  Row  " + (e.ColumnIndex + 1) + "  Column button clicked ");
    }
}


推荐答案

I已尝试您的样本,它的工作原理。您确实将事件绑定到 DataGridView

I have tried your sample and it works. Did you really bind the event to your DataGridView ?

请检查 InitializeComponent < YourFormName> .Designer.cs 类中的方法。
它真的有

Please check the InitializeComponent() Method in your <YourFormName>.Designer.cs class. Did it really has

this.dataGridView1.CellClick += 
  new System.Windows.Forms.DataGridViewCellEventHandler(this.dataGridView1_CellClick);

并且你不会在代码中的另一个地方删除处理程序?

and you don't remove the handler at another place in code ?

MSDN页面中的DataGridViewButtonColumn可以这样说:

The DataGridViewButtonColumn MSDN page has this to say:


按钮点击,处理DataGridView.CellClick或DataGridView.CellContentClick事件。在事件处理程序中,您可以使用DataGridViewCellEventArgs.ColumnIndex属性来确定是否在按钮列中发生了点击。您可以使用DataGridViewCellEventArgs.RowIndex属性来确定点击是否发生在按钮单元格中,而不是列标题上。

To respond to user button clicks, handle the DataGridView.CellClick or DataGridView.CellContentClick event. In the event handler, you can use the DataGridViewCellEventArgs.ColumnIndex property to determine whether the click occurred in the button column. You can use the DataGridViewCellEventArgs.RowIndex property to determine whether the click occurred in a button cell and not on the column header.

这篇关于datagridview按钮列Winforms的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆