C# DataGridView.CellContentClick

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/381072/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 01:20:02  来源:igfitidea点击:

DataGridView.CellContentClick

c#winforms

提问by Cornel

DataGridView.CellContentClick is not firing if I mouse click a DataGridViewCheckBoxCell very fast. How can I solve this? I need to know when CheckBox's check state changes

如果鼠标单击 DataGridViewCheckBoxCell 的速度非常快,则 DataGridView.CellContentClick 不会触发。我该如何解决这个问题?我需要知道 CheckBox 的检查状态何时发生变化

采纳答案by Colby Africa

Try handling the CellMouseUpevent.
You can check which colum the MouseUpevent occurred in to see if it is your checkbox column.
You can also find out if it is in edit mode and end the edit mode programmatically, which in turn will fire the CellValueChangedevent.

尝试处理CellMouseUp事件。
您可以检查MouseUp事件发生在哪个列以查看它是否是您的复选框列。
您还可以确定它是否处于编辑模式并以编程方式结束编辑模式,这反过来将触发CellValueChanged事件。

In the example below, I have a DataGridView with two colums.
The first is a DataGridViewTextBoxColumnand the second is a DataGridViewCheckBoxColumn.
When the checkbox changes, the first column wil reflect its check state, without having to move from the row or cell.

在下面的示例中,我有一个带有两个列的 DataGridView。
第一个是a DataGridViewTextBoxColumn,第二个是a DataGridViewCheckBoxColumn
当复选框更改时,第一列将反映其选中状态,而无需从行或单元格中移动。

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        dataGridView1.Rows.Add("False", false);
        dataGridView1.Rows.Add("True", true);
    }

    private void dataGridView1_CellMouseUp(object sender, DataGridViewCellMouseEventArgs e)
    {
        if (e.ColumnIndex == 1 && e.RowIndex >-1 && dataGridView1.Rows[e.RowIndex].Cells[1].IsInEditMode)
        {
            dataGridView1.EndEdit();
        }
    }

    private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
    {
        if (e.RowIndex != -1)
        {
            dataGridView1.Rows[e.RowIndex].Cells[0].Value =
               dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString(); 
        }
    }
}

回答by BFree

Regardless of how fast the user clicks in the checkbox cell, the value won't change from true to false or vise versa until they click out of that row, and the DataGridView goes out of edit mode.

无论用户在复选框单元格中单击多快,值都不会从 true 变为 false 或反之亦然,直到他们单击该行之外,并且 DataGridView 退出编辑模式。

What I've done in the past, is set that column to ReadOnly = true. Then, in the CellContentClick event handler, if that column was clicked, I manually flipped the bool like this:

我过去所做的是将该列设置为 ReadOnly = true。然后,在 CellContentClick 事件处理程序中,如果单击该列,我会像这样手动翻转 bool:

bool b = (bool)this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value;
this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = !b;

Then, you can do your logic at this point that you would normally do for the CheckChanged.

然后,您可以在此时执行您通常为 CheckChanged 执行的逻辑。

回答by Mikhail Semenov

It's better to handle the event CellContentClick(if you click accidentally outside the box itself, it won't work properly):

最好处理事件CellContentClick(如果您不小心在框本身之外单击,它将无法正常工作):

grid.CellContentClick += delegate(object obj, DataGridViewCellEventArgs args)
{
    var cell = (settings_grid[args.ColumnIndex,args.RowIndex] as DataGridViewCheckBoxCell);

    if (cell != null)
    {
        bool new_value = !(bool)cell.Value;                                        
        RecordTheNewState(new_value); // you record the new checkbox state
    }
};

回答by KyleMit

I'm a little late to the party, but msdn has a very good answer to this problem here.

我参加聚会有点晚了,但是 msdn在这里对这个问题有一个很好的答案。

'Ends Edit Mode So CellValueChanged Event Can Fire
Private Sub EndEditMode(sender As System.Object, e As EventArgs) _
    Handles DataGridView1.CurrentCellDirtyStateChanged
    'if current cell of grid is dirty, commits edit
    If DataGridView1.IsCurrentCellDirty Then
        DataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit)
    End If
End Sub

I also wrote up a very detailed elaboration of the msdn fix and those found here on the post Firing The DataGridView CellValueChanged Event Immediately

我还写了一篇非常详细的 msdn 修复以及在Firing The DataGridView CellValueChanged Event 一文中找到的那些