【问题标题】:What is the best way to add action to datagrid column button?向数据网格列按钮添加操作的最佳方法是什么?
【发布时间】:2011-10-27 13:51:55
【问题描述】:

您好,我有一个 Windows 窗体应用程序,我有一个数据网格,最后一列是按钮列。我已经读过,为了能够回答单击事件,我必须使用“cellClick”事件处理程序,但是当我使用它时,无论是单击按钮还是单击角落中的单元格,事件都会触发。那么有没有更好的方法和方法。提前感谢您的帮助。

【问题讨论】:

  • 您是否像在这个问题中一样使用 DataGridView 和 DataGridViewButtonColumn ? stackoverflow.com/questions/6085930/…
  • 是的,我正在使用 dataGridView 和 DataGridViewButtonColumn 。

标签: c# winforms datagridview


【解决方案1】:

我假设您正在使用数据网格视图,那么这是解决方案

这是捕获按钮单击事件所需的 DataGridView 处理程序。

this.dgvList.CellContentClick += new DataGridViewCellEventHandler(DGV_CellContentClick);

这是按钮单击处理程序示例

public void DGV_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
    int selectedRowIndex = int.Parse(e.RowIndex.ToString());

    if (this.dgvList.Columns[e.ColumnIndex] == buttonColumn && selectedRowIndex >= 0)
    {
        //do what ever you want
       // DataRow dr = DataGridViewHelper.GetDataRow(this.dgvList);
        //MessageBox.Show((string)dr["FirstName"]);
    }
}

希望对你有帮助....

【讨论】:

    猜你喜欢
    • 2013-08-21
    • 2014-09-10
    • 1970-01-01
    • 2010-09-24
    • 2017-09-16
    • 2018-05-27
    • 1970-01-01
    • 1970-01-01
    • 2015-03-12
    相关资源
    最近更新 更多