C#DataGridView(CheckBox)单元格单击多个回调

时间:2012-08-18 16:42:41

标签: c# datagridview checkbox callback toggle

[C#,Visual Studio 2008,Windows 7 64]

我班上有一个 DataGridView 。此数据网格视图使用 DataGridViewCheckBoxColumn ,以便每个单元格都包含一个复选框。

以下是其中一行的屏幕截图:

DataGridView Checkboxes

我希望能够检测用户是否选择了单元格(单元格中的某个位置但不在复选框的顶部)。我还想检测用户选择复选框。为了做到这一点,我的代码必须为这两个事件设置回调:

this.CellClick += cellClick; // callback when the user selects a cell
this.CellContentClick += cellContentClick; // callback when the user selects a checkbox

以下是回调方法:

    private void cellContentClick(object sender, DataGridViewCellEventArgs e)
    {
        toggleCellCheck(e.RowIndex, e.ColumnIndex);
    }

    private void cellClick(object sender, DataGridViewCellEventArgs e)
    {
        toggleCellCheck(e.RowIndex, e.ColumnIndex);
    }

    private void toggleCellCheck(int row, int column)
    {
        bool isChecked = (bool)this[column, row].EditedFormattedValue;
        this.Rows[row].Cells[column].Value = !isChecked;
    }

注意:正如您所见, toggleCellCheck 方法获取复选框值并切换,检查 - >取消选中或取消选中 - >检查。)

当用户单击不是复选框的单元格中的任何位置时,只会触发一个回调, cellClick 。随后调用 toggleCellCheck 方法,并且复选框状态翻转。

这是我想要的确切行为。

我遇到的问题是,当用户直接点击复选框时,两个事件将按以下顺序触发: cellClick 然后 cellContentClick

正在执行的两个回调都会导致在第一次回调后切换已检查状态,然后在第二次回调后再次切换。最后的结果当然是复选框已检查状态不会改变。

有没有什么方法可以配置 DataGridView 类来阻止两个回调被触发?或者,有没有一种方法可以检测(在 cellContentClick 方法内)这是第二个回调,或者通过单击复选框生成回调,然后退出没有调用 toggleCellCheck

我在考虑以下内容:

private void cellContentClick(object sender, DataGridViewCellEventArgs e)
{
    // if sender/sender child/etc. is of type checkbox then return because
    // _cellClick_ has already been called to change the checkbox checked property
    // something like the following:
    //
    // if (typeof(sender) == CheckBox) return;
    // else toggleCellCheck(e.RowIndex, e.ColumnIndex);
}

谢谢!

2 个答案:

答案 0 :(得分:3)

您不应该需要单元格内容点击处理程序 - 选中复选框时会调用单元格单击。

您的最终目标似乎是让网格响应所点击的单元格内容,以及实际的复选框点击 1

要执行此操作,只需使用以下内容附加到单元格单击事件:

void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
    if (dataGridView1.Columns[e.ColumnIndex].Name == "checkboxcolumn")
    {
        Console.WriteLine("Click");
        bool isChecked = (bool)dataGridView1[e.ColumnIndex, e.RowIndex].EditedFormattedValue;
        dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = !isChecked;
        dataGridView1.EndEdit();
    }
}

1。我建议不要使用这种ui修改 - 像DataGridView这样的控件的默认行为是广泛传播和经过充分测试的。改变它们通常是一个坏主意。

答案 1 :(得分:0)

private void dataGridView2_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
    DataGridViewCheckBoxCell dgvcell = (DataGridViewCheckBoxCell)dataGridView2[e.ColumnIndex, e.RowIndex]; 
    if ( ( Convert.ToBoolean(dataGridView2[e.ColumnIndex, e.RowIndex].Value ) == true ) )
             { 
                 dgvcell.Value = CheckState.Unchecked;
                 lst_box2.Items.Add(dataGridView2.Rows[e.RowIndex].Cells["ItemName"].Value.ToString());
                 lst_box1.Items.Remove(dataGridView2.Rows[e.RowIndex].Cells["ItemName"].Value.ToString());
            } 
            else 
            { 
                lst_box1.Items.Add(dataGridView2.Rows[e.RowIndex].Cells["ItemName"].Value.ToString());
                lst_box2.Items.Remove(dataGridView2.Rows[e.RowIndex].Cells["ItemName"].Value.ToString());
                dgvcell.Value = CheckState.Checked;
            } 

}
相关问题