如何触发GridView_CellContentClick事件?

时间:2014-10-31 08:36:17

标签: c# winforms checkbox datagridview

我有一个WinForms应用程序。在DataGridView中,我使用以下代码动态生成了一个复选框列:

DataGridViewCheckBoxColumn myCheckedColumn = new DataGridViewCheckBoxColumn()
    {
        Name = "My column",
        FalseValue = 0,
        TrueValue = 1,
        Visible = true
    };

mydatagridview.Columns.Insert(0, myCheckedColumn);

当我点击一个复选框时,我想执行验证检查,因为只检查一个复选框,而不是多个复选框。

我尝试使用事件mydatagridview_CellContentClick,但我无法触发此事件。

我编写的事件代码如下:

private void mydatagridview_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
    if ((sender as DataGridView).CurrentCell is DataGridViewCheckBoxCell)
    {
        if (Convert.ToBoolean(((sender as DataGridView).CurrentCell as DataGridViewCheckBoxCell).Value))
        {
            int currentcolumnclicked = e.ColumnIndex;
            int currentrowclicked = e.RowIndex;

            foreach (DataGridViewRow dr in associatinggridView.Rows)
            {
                dr.Cells[currentcolumnclicked].Value = false;
            }

            associatinggridView.CurrentRow.Cells[currentrowclicked].Value = true;
        }
    }
}

1 个答案:

答案 0 :(得分:0)

你必须使用CellValueChanged事件和CurrentCellDirtyStateChanged事件......请在这里查看我的答案,它在vb中,但你只需要事件。

Displaying number of rows checked/unchecked on datagridview

相关问题