Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# DataGridView';s按钮单击事件_C#_Datagridview - Fatal编程技术网

C# DataGridView';s按钮单击事件

C# DataGridView';s按钮单击事件,c#,datagridview,C#,Datagridview,我有一个DataGridView,我使用DataAdapter+DataTable填充它。 我使用以下代码创建了一个按钮: 此外,我尝试了以下方法: private void dgv_tipo_despesas_CellClick_1(object sender, DataGridViewCellEventArgs e) { if (e.RowIndex < 0 || dgv_tipo_despesas.cell) return; { ///Execu

我有一个DataGridView,我使用DataAdapter+DataTable填充它。
我使用以下代码创建了一个按钮:

此外,我尝试了以下方法:

private void dgv_tipo_despesas_CellClick_1(object sender, DataGridViewCellEventArgs e)
  {
    if (e.RowIndex < 0 || dgv_tipo_despesas.cell) return;
    {
         ///ExecuteSomeCode
    }
  }
private void dgv_tipo_despesas_CellClick_1(对象发送方,DataGridViewCellEventArgs e)
{
如果(e.RowIndex<0 | | dgv_tipo_despeas.cell)返回;
{
///执行代码
}
}
但它给了我一个错误:*对象引用未设置为对象的实例*

当按下/单击这些按钮时,我需要执行一些代码。我已经通过以下活动实现了这一目标:

  • DataGridView\u单元单击
  • DataGridView\u单元格内容单击

他们都很好,但是。。。当我单击列的标题时,它也会被激发。我想要一个只在点击按钮时触发的事件。。。是否有?

您应该能够使用这些事件,但需要检查e.Source(或类似的内容)是否是您想要单击的单元格之一。

根据MSDN,它看起来不像它,因为标题也是一行

您可以使用DataGridViewCellEventArgs.RowIndex属性确定单击是否发生在按钮单元格中,而不是列标题上


我找到了解决方案,我只需要检查它是否不是DataGridViewButtomColumn和.RowIndex。像这样:

if (!(MyDataGridView.Columns[e.ColumnIndex] is DataGridViewButtonColumn && e.RowIndex != -1)) return;
    {

       //MyCodeHere
    }

你能给我举个例子吗?我在链接上尝试了这个例子,当我点击标题时,它运行良好,返回;。但是当我点击一个普通的行时,它给了我这个错误:对象引用没有设置为对象的实例。我尝试了以下代码:if(e.RowIndex<0 | | e.ColumnIndex!=dataGridView1.Columns[“Status Request”].Index)返回;
if (!(MyDataGridView.Columns[e.ColumnIndex] is DataGridViewButtonColumn && e.RowIndex != -1)) return;
    {

       //MyCodeHere
    }