Hi all, I am working on a project affiliated to customer's database and i am required to insert, update, delete the data through a window's form program. The database i am using is MS SQL server 2005 and one of the columns i had is "Selection" which its datatype is set to "bits" which is used as a checkbox control in the datagridview in my program. The checkbox allows me to choose the specific row of customer's data, which i append to textboxes, i am able restrict the datagridview checkbox column to only be able to select only one checkbox/row of data at a time using "DataGridView1_CellValueChanged" event but the results seemed to have made the values of all the rows of data to "false" when a checkbox is "checked" and i cant display my customer's data to textboxes due to that. Can someone tell me how to set the value of the row of data of the "checked" checkbox to "true", while the others which are NOT "checked" to "false"? I have attached the codes to give a clearer picture of what i meant :- Thanks, Charles Note: 6 is the column index of my checkbox in the database
Private CheckColIndex As Integer = 6
Private Sub DataGridView1_CellValueChanged(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellValueChanged
'Check the column index and if the check box is checked.
If e.ColumnIndex = CheckColIndex Then
Dim isChecked As Boolean = CType(Me.DataGridView1(e.ColumnIndex, e.RowIndex).Value, Boolean)
If isChecked Then
'If check box is checked, uncheck all the rows, the current row would be checked later.
For Each row As DataGridViewRow In Me.DataGridView1.Rows
row.Cells(e.ColumnIndex).Value = False
Next
End If
End If
End Sub
| | Charles Mohd Monday, August 03, 2009 8:23 AM | Hi Charles,
We can handle the CellContentClick event instead. CellValueChanged event is not always fired when we check a cell, but CellContentClick does. This is the code snippet:
'If you check a cell, this event would always be fired.
Private Sub DataGridView1_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
'Check the column index and if the check box is checked.
If e.ColumnIndex = CheckColIndex Then
Dim isChecked As Boolean = CType(Me.DataGridView1(e.ColumnIndex, e.RowIndex).Value, Boolean)
'If the value is false, it means it would be checked.
If Not isChecked Then
'If check box is checked, uncheck all the rows, the current row would be checked later.
For Each row As DataGridViewRow In Me.DataGridView1.Rows
If CType(row.Cells(e.ColumnIndex).Value, Boolean) = True And row.Index <> e.RowIndex Then
'Set other checked cells unchecked.
row.Cells(e.ColumnIndex).Value = False
End If
Next
End If
End If
End Sub
Let me know if this helps. Aland Li
Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread. - Marked As Answer byCharles Mohd Wednesday, August 05, 2009 3:24 AM
-
| | Aland Li Wednesday, August 05, 2009 3:04 AM | Hi Charles,
We can handle the CellContentClick event instead. CellValueChanged event is not always fired when we check a cell, but CellContentClick does. This is the code snippet:
'If you check a cell, this event would always be fired.
Private Sub DataGridView1_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
'Check the column index and if the check box is checked.
If e.ColumnIndex = CheckColIndex Then
Dim isChecked As Boolean = CType(Me.DataGridView1(e.ColumnIndex, e.RowIndex).Value, Boolean)
'If the value is false, it means it would be checked.
If Not isChecked Then
'If check box is checked, uncheck all the rows, the current row would be checked later.
For Each row As DataGridViewRow In Me.DataGridView1.Rows
If CType(row.Cells(e.ColumnIndex).Value, Boolean) = True And row.Index <> e.RowIndex Then
'Set other checked cells unchecked.
row.Cells(e.ColumnIndex).Value = False
End If
Next
End If
End If
End Sub
Let me know if this helps. Aland Li
Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread. - Marked As Answer byCharles Mohd Wednesday, August 05, 2009 3:24 AM
-
| | Aland Li Wednesday, August 05, 2009 3:04 AM | Hi Aland, thank you so much, it worked. | | Charles Mohd Wednesday, August 05, 2009 3:26 AM | Hi Charles,
You are welcome. Sorry for not completely solving your issue in your last thread.
Aland Li Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread. | | Aland Li Wednesday, August 05, 2009 3:28 AM | No problem, i can understand, its practically impossible to resolve all issues within threads due to the time constraint and the amount of new threads posted every single day. | | Charles Mohd Wednesday, August 05, 2009 3:48 AM |
|