DataGridViewButtonCell需要单击两次以触发事件

时间:2018-07-27 13:31:54

标签: c# button datagridview

我创建了一个表单,该表单主要由dataGridView组成,最右边的列是DataGridViewButtonColumn,并且正在CellContentClicked处理程序中处理该事件。

我的问题是,当单击列中的按钮一次时,不会触发任何事件,但是,对于每一行,如果再次单击按钮,则会触发CellContentClicked事件。例如,如果用户浏览整个行列表并单击每个按钮一次,则不会触发任何事件,但是,如果用户随后又单击并单击任何已单击的按钮,则事件将触发。

有什么想法为什么dataGridView正在等待第二次单击?它在等待某种关注吗?

谢谢!

编辑:我尝试使用帖子DataGridView - "Cell Selection Style" - Edit Cell中提供的解决方案,但是这些解决方案未能成功解决我的问题:(

处理程序代码如下:

private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
    var senderGrid = (DataGridView)sender;// Cast sender to DataGridView type

    if (senderGrid.Columns[e.ColumnIndex] is DataGridViewButtonColumn && e.RowIndex >= 0)// check column is button column
    {

        if (dataGridView1.Rows[e.RowIndex].Cells[0].Style.BackColor == SystemColors.Window)
        {
            foreach (DataGridViewCell cell in dataGridView1.Rows[e.RowIndex].Cells)
            {
                cell.Style.BackColor = Color.AliceBlue;
            }
        }
        else
        {
            foreach (DataGridViewCell cell in dataGridView1.Rows[e.RowIndex].Cells)
            {
                cell.Style.BackColor = SystemColors.Window;
            }
        }
    }
}

0 个答案:

没有答案