DataGridView.CellContentClick

DataGridView.CellContentClick does not work if I quickly click on DataGridViewCheckBoxCell. How can i solve this? I need to know when CheckBox state changes are checked

+2
source share
4 answers

Try to handle the CellMouseUp event. You can check in which column the MouseUp event occurred to see if this 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 trigger the CellValueChanged event.

In the example below, I have a two-column datagridview. The first is the column of the text box, and the second is the column of the check box. When the checkbox changes, the first wil column reflects its check state without moving from a row or cell.

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(); } } } 

NTN

Colby africa

+5
source

No matter how fast the user clicks on the check box, the value will not change from true to false or vice versa until they exit this line and the DataGridView exits edit mode.

What I did in the past is set in the ReadOnly = true column. Then in the CellContentClick event handler, if this column was clicked, I manually turned over the bool as follows:

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

You can then do your logic at this point, which is what you usually do for CheckChanged.

+4
source

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

 '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 a very detailed description of the msdn fix and the EventGridView CellValueChanged immediate event Failure found here in the message

+2
source

It is better to handle the CellContentClick event (if you accidentally click outside the window itself, it will not work properly):

 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 } }; 
+1
source

Source: https://habr.com/ru/post/907726/


All Articles