在DataGridView的Click事件中写如下代码
MessageBox.Show(dataGridView1.CurrentRow.Index.ToString());可是在我点击DataGridView的标题栏时也弹出了一个数字,这个数字是我最后点击的那一行。不想让点击标题栏也触发事件,怎么处理?
MessageBox.Show(dataGridView1.CurrentRow.Index.ToString());可是在我点击DataGridView的标题栏时也弹出了一个数字,这个数字是我最后点击的那一行。不想让点击标题栏也触发事件,怎么处理?
Click事件是点击控件就会触发
CellContentClick是点击单元格
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex >= 0)
{
//你要的操作
}
}
你确定你跟我写的一样?
主要是这句话,你可以试一下!
header的index是-1!
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex >= 0 && e.ColumnIndex >= 0)
{
MessageBox.Show(e.RowIndex.ToString()+ " , " + e.ColumnIndex.ToString());
}
}
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex >= 0 && e.ColumnIndex != -1)
{
//你要的操作
}
}9楼的方法是可行的,改成这样点击行头的时候也就不会触发事件了