DataGridview.CellContentClick出错

时间:2011-11-08 05:56:33

标签: c# datagridview

我正在处理单元格内容点击事件。 当用户单击其准确捕获的单元格内容时。

考虑这种情况,我在第3列有DataGridViewLinkColumn 但是在单击一个单元格(比如第1行第3列)后,当用户意外地点击任何表格Header时,将保留cellclick事件,即它使用相同的RowIndex和ColumnIndex(第1行和第3列)调用CellContentClicked事件。

如何避免这种情况? 请帮助..

3 个答案:

答案 0 :(得分:1)

我找到了答案.. 单击表标题时,将针对上一个选定单元格单击单元格。

我们可以通过添加条件

来限制此功能

if(e.CoumnIndex> = 0&& e.RowIndex> = 0)

{

// Add  Logic neccessary for Cell Click event

}

(对于标题行,rowIndex为-1)

答案 1 :(得分:0)

这基本上是因为上一个/默认选择的行。要解决此问题,您可以使用默认选择属性设置为none来定义控件。

就像列表视图一样,我使用的是:

<ListView ItemContainerStyle="{StaticResource listViewStyle}" SelectedIndex=-1 .. />

答案 2 :(得分:0)

对于Windows窗体应用程序,

if (e.ColumnIndex >= 0 && e.RowIndex >= 0)
{         
    // Add  Logic  for Cell Click event
}
else
{
   MessageBox.Show("Error Message", "My Application",
   MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
}