Changhong's Technical Blog

  Home  |   Contact  |   Syndication    |   Login
  16 Posts | 0 Stories | 50 Comments | 3 Trackbacks

News

Archives

Post Categories

Blogs I read from kiwis...

Blogs I read...

I had this problem today. It looks very simple, but actually took me a while to find a solution. The problem is: I have a Winforms DataGridView, and its first column is an unbound CheckBox column which is used to select/unselect a row. I want to hook up the check/uncheck event so that I can execute some logic after a row is selected or unselected. As this is Winforms, there is no way I can hook up an event from a control’s child element. But, it is quite obvious that we can hook up DataGridView’s OnCellvalueChanged event and put the logic there. So I had some code like this:

private void EmployeesGrid_OnCellValueChanged(object sender,  

     DataGridViewCellEventArgs e)

{

     if (e.ColumnIndex == 0 && e.RowIndex > -1)

     {

        bool selected = (bool)_gvEmployees[e.ColumnIndex, e.RowIndex].Value;

        _gvEmployees.Rows[e.RowIndex].DefaultCellStyle.BackColor =

selected ? Color.Yellow : Color.White;

     }

}

 

Here, for demonstration, I just simple change the background color of a row depending on if the checkbox column is checked or not. It all works fine except the background color won’t change until you move your mouse cursor out of the cell. The reason for that is OnCellvalueChanged event won’t fire until the DataGridView thinks you have completed editing. This makes senses for a TextBox Column, as OnCellvalueChanged wouldn’t broth to fire for each key strike, but it doesn’t for a CheckBox.

 

After trying a few different events, I finally find a workaround, and it is very simple. I just need to hook up on CellMouseUp event and explicitly exit edit mode there. Some sample code is here:

 

private void EmployeesGrid_OnCellMouseUp(object sender,  

     DataGridViewCellMouseEventArgs e)

{

     if (e.ColumnIndex == 0 && e.RowIndex > -1)

     {

        _gvEmployees.EndEdit();

     }

}

posted on Sunday, September 07, 2008 8:29 PM

Feedback

# re: How to get a CheckBox in a Winforms DataGridView to react to the first click 12/3/2008 2:54 AM Kevin
Converted the code to VB.NET and threw my event after the "end edit" and it worked the value is validated right after the click instead of when the next cell is selected.

Thanks a million you have saved me so much hassle.

Thanks
Kev

# re: How to get a CheckBox in a Winforms DataGridView to react to the first click 4/30/2009 7:42 PM rg
How did you hook up the "OnCellMouseUp" Cause it never fires

# re: How to get a CheckBox in a Winforms DataGridView to react to the first click 6/15/2009 10:21 AM Jean
Well done, you have just saved me a lot of time !

here it is in vb.net

Private Sub dgrTrades_CellValueChanged(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgrTrades.CellValueChanged

End Sub

Private Sub dgrTrades_CellMouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles dgrTrades.CellMouseUp
If e.RowIndex > -1 Then
dgrTrades.EndEdit()
End If


End Sub

# re: How to get a CheckBox in a Winforms DataGridView to react to the first click 6/22/2009 10:57 PM Michaël Van Laere
What if the user changes the state of the checkbox by using the keyboard?

# re: How to get a CheckBox in a Winforms DataGridView to react to the first click 10/26/2009 4:52 AM Drishty
Thanks, It helps a lot...

# re: How to get a CheckBox in a Winforms DataGridView to react to the first click 11/23/2009 4:30 AM Arapi
You dont actually need to code CellMouseUp event because if the user is using keyboard instead of the mosue the logic will not execute. You can write your logic in CellContentClick event, like this:
Private Sub dg_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dg.CellContentClick
If e.ColumnIndex = 0 AndAlso Not dg.Rows(e.RowIndex).IsNewRow Then
dg.EndEdit()
' logic goes here
End Sub

Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification: