DataGridView的CellClick事件

时间:2011-10-13 09:36:54

标签: c# winforms

即使我们单击DataGridView的标题,单元格单击事件也会被触发。如何阻止它开火?

2 个答案:

答案 0 :(得分:3)

比较RowIndexColIndex,如果它们的值为-1(标题列/行索引),则为ignore

testDataGridView.CellClick += (senderObject, eventArgs) =>
 {
  if (eventArgs.RowIndex == -1 || eventArgs.ColumnIndex == -1)
    return;
  //statements here
 };

答案 1 :(得分:1)

    private void DataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
    {
        if (e.RowIndex == -1)
        {                
            return;                
        }
       //Write your code here ...
    }