|
-
Jun 6th, 2010, 03:04 PM
#1
[RESOLVED] DataGridView - event when user edits checkbox cell?
Hi,
I'm afraid this is a very simple question, but I can't figure it out... I hardly ever use the DataGridView (usually use a third party grid) so I never needed to do this before.
Anyway, I have a DataGridView that is data-bound to a List(Of SomeClass). The grid is not ReadOnly, but I made every column ReadOnly except the very first, which is a boolean column so it displays a checkbox.
This way, the user cannot edit any cell value, but he can check/uncheck the checkbox in the first column.
What I need now basically is an event that is raised when the user (not programmatically) changes the value of this checkbox. I also need to be able to retrieve the bound item from that event (from the row number I think?) but that should be no problem, I hope.
Anyway, there's a bunch of events that looked promising but all have something I don't want:
1. CellBeginEdit - Fires before the checkbox value has changed, so can't use this.
2. CellEndEdit - Only fires after the edited row loses focus. I want to save it back to the database as soon as the user changes the checkbox value!
3. CellValueChanged - Same problem as nr 2.
I'm sure it must be possible for me to react as soon as the user changes the value of the checkbox? How can I do this?
-
Jun 6th, 2010, 04:15 PM
#2
Re: DataGridView - event when user edits checkbox cell?
What about the CellContentClick event? Is that of any help to you?
-
Jun 6th, 2010, 04:20 PM
#3
Re: DataGridView - event when user edits checkbox cell?
Maybe... But I suppose it's possible to click the cell without changing the state of the checkbox? I don't want to save the value too often, so if it's possible I only want to save it when it actually changed...
-
Jun 6th, 2010, 04:24 PM
#4
Re: DataGridView - event when user edits checkbox cell?
No that event fires only if you click the CheckBox etc. inside it, not by clicking in the empty area. Just put that event and put a breakpoint there and do some hits and trials and you would get an idea.
-
Jun 6th, 2010, 07:18 PM
#5
Re: DataGridView - event when user edits checkbox cell?
This is from the documentation for the DataGridViewCheckBoxColumn class:
Typically, check box cell values are intended either for storage, like any other data, or for performing bulk operations. If you want to respond immediately when users click a check box cell, you can handle the DataGridView.CellContentClick event, but this event occurs before the cell value is updated. If you need the new value at the time of the click, one option is to calculate what the expected value will be based on the current value. Another approach is to commit the change immediately, and handle the DataGridView.CellValueChanged event to respond to it. To commit the change when the cell is clicked, you must handle the DataGridView.CurrentCellDirtyStateChanged event. In the handler, if the current cell is a check box cell, call the DataGridView.CommitEdit method and pass in the Commit value.
-
Jun 7th, 2010, 02:37 AM
#6
Re: DataGridView - event when user edits checkbox cell?
I see. I thought the CellContentClick event would be raised even when you click outside the checkbox, in which case I couldn't determine what the new state of the checkbox would be (toggled if the user clicked inside the checkbox, or still the old value if he didn't), but apparently it raises only when the checkbox is clicked, so I can determine the new state.
Anyway, now that I know this, I can't seem to get the value of the checkbox..?
I tried getting the clicked Cell and displaying its Value property, and I even tried casting the cell to a DataGridViewCheckBoxCell before using the Value, but it always returns what it was initially, and it does not change when the checkbox is changed.
vb.net Code:
Private Sub Grid1_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles Grid1.CellContentClick If e.ColumnIndex < 0 OrElse e.RowIndex < 0 Then Exit Sub Dim col = Grid1.Columns(e.ColumnIndex) Dim row = Grid1.Rows(e.RowIndex) If col.Name = "Bool" Then Dim chkCell = DirectCast(row.Cells("Bool"), DataGridViewCheckBoxCell) MessageBox.Show(chkCell.Value.ToString()) End If End Sub
For example, if the first row has a checked checkbox, when I click it to change it to an unchecked checkbox, the messagebox displays 'True'. That made sense to me, because the event is raised before the change is made. Ok. But then I click it again, and it still displays True, even when it was False before I clicked it..? I must be missing something... Shouldn't I use the Value property?
-
Jun 7th, 2010, 02:52 AM
#7
Re: DataGridView - event when user edits checkbox cell?
The issue arises because the cell Value doesn't actually change until you navigate to another cell. Read the last two sentences of the quote in my last post again.
-
Jun 7th, 2010, 03:17 AM
#8
Re: DataGridView - event when user edits checkbox cell?
vb.net Code:
Private Sub DataGridView1_CellContentClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick If DataGridView1.Columns(e.ColumnIndex).Name = "Bool" Then DataGridView1.CommitEdit(Nothing) 'test to see if we now have the new value MsgBox(DataGridView1.SelectedCells(0).Value) End Sub
-
Jun 7th, 2010, 11:11 AM
#9
Re: DataGridView - event when user edits checkbox cell?
Thanks, it works after using CommitEdit.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|