Answered by:
DataGridView, Checkbox Column Question

Question
-
So... I have a DataGridView with a checkbox column. How do I capture its check event. The CellValueChanged event fires, but only after a different cell is clicked.
Thanks!
Thanks,
Corey Furman @ Facebook
______________________________________________________
Please mark posts as answer or helpful when they are.Tuesday, February 10, 2009 10:17 PM
Answers
-
Try the following. Please note there are two checks for column type, only one is needed. Seems that .EndEdit is the key
Private Sub DataGridDemo_CellContentClick(ByVal sender As Object, _ ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) _ Handles DataGridView1.CellContentClick Dim GridView As DataGridView = DirectCast(sender, DataGridView) If TypeOf GridView.CurrentCell Is DataGridViewCheckBoxCell Then Console.WriteLine("Checked value is {0}", _ GetValueOfActionField(GridView, "CheckList")) End If End Sub Function GetValueOfActionField(ByVal GridView As DataGridView, _ ByVal FieldName As String) As Boolean With GridView If TypeOf .CurrentCell Is DataGridViewCheckBoxCell Then .EndEdit() ' Must be here else you will not get the proper value Return Convert.ToBoolean(.Rows(.CurrentRow.Index).Cells(FieldName).Value) Else Throw New Exception(FieldName & "is not a CheckBox column") End If End With End Function
KSG- Marked as answer by Corey Furman Wednesday, February 11, 2009 6:23 PM
Wednesday, February 11, 2009 5:51 PM
All replies
-
BTW, I could swear I posted this question hours ago, but I cannot find it. If I've double posted, please let me know.
Thanks.
Thanks,
Corey Furman @ Facebook
______________________________________________________
Please mark posts as answer or helpful when they are.Tuesday, February 10, 2009 10:19 PM -
Try the following. Please note there are two checks for column type, only one is needed. Seems that .EndEdit is the key
Private Sub DataGridDemo_CellContentClick(ByVal sender As Object, _ ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) _ Handles DataGridView1.CellContentClick Dim GridView As DataGridView = DirectCast(sender, DataGridView) If TypeOf GridView.CurrentCell Is DataGridViewCheckBoxCell Then Console.WriteLine("Checked value is {0}", _ GetValueOfActionField(GridView, "CheckList")) End If End Sub Function GetValueOfActionField(ByVal GridView As DataGridView, _ ByVal FieldName As String) As Boolean With GridView If TypeOf .CurrentCell Is DataGridViewCheckBoxCell Then .EndEdit() ' Must be here else you will not get the proper value Return Convert.ToBoolean(.Rows(.CurrentRow.Index).Cells(FieldName).Value) Else Throw New Exception(FieldName & "is not a CheckBox column") End If End With End Function
KSG- Marked as answer by Corey Furman Wednesday, February 11, 2009 6:23 PM
Wednesday, February 11, 2009 5:51 PM -
Good job!
Thanks
Thanks,
Corey Furman @ Facebook
______________________________________________________
Please mark posts as answer or helpful when they are.Wednesday, February 11, 2009 6:24 PM -
If you are using VS2008 the following extension might be worth looking at
<Runtime.CompilerServices.Extension()> _ Function CurrentRowCheckBoxValue( _ ByVal GridView As DataGridView, _ ByVal ColumnName As String) As Boolean GridView.EndEdit() ' Must be here else you will not get the proper value Return Convert.ToBoolean( _ GridView.Rows(GridView.CurrentRow.Index).Cells(ColumnName).Value) End Function
So in CellContentClick event
Private Sub DataGridDemo_CellContentClick() _ Handles DataGridView1.CellContentClick If dgv.CurrentRowCheckBoxValue("CheckList") Then End If End Sub
KSGThursday, February 12, 2009 3:11 PM