我有一个包含9列的DataGridView。列索引4和8是单元格按钮。当我点击按钮索引4时,它将执行给定的命令,但也将执行为按钮索引8给出的命令。无论哪种方式按钮我单击(4或8)它将始终执行动作1然后动作2
private void dgvItems_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
var senderGrid = (DataGridView)sender;
if (senderGrid.Columns[4] is DataGridViewButtonColumn && senderGrid.Rows[e.RowIndex] is DataGridViewRow)
{
MessageBox.Show("ACTION 1: Column index is " + e.ColumnIndex + "; Row Index " + e.RowIndex);
}
if (senderGrid.Columns[8] is DataGridViewButtonColumn && senderGrid.Rows[e.RowIndex] is DataGridViewRow)
{
MessageBox.Show("ACTION 2: Column index is " + e.ColumnIndex + "; Row Index " + e.RowIndex);
}
}
答案 0 :(得分:1)
只需按e.ColumnIndex
检查当前列。将if
条件更正为:
if (e.ColumnIndex == 4)
MessageBox.Show("ACTION 1: Column index is " + e.ColumnIndex + "; Row Index " + e.RowIndex);
if (e.ColumnIndex == 8)
....