|
-
Dec 12th, 2015, 05:37 PM
#1
Thread Starter
Member
Mutually exclusive checkbox columns in datagridview
Hi!
I need your help one more time, experts!
I have a bound datagridview pulling data in from SQL Server 2008 db. I have 4 columns in my database that allow for bit values.
When the datagridview is populated, it shows the checkboxes for those 4 columns. I need to make them mutually exclusive, i.e. if someone clicks on checkbox from column 15, then they cannot select another one, if they do, it will uncheck column 15 and check the one they choose next.
I found this code online, but it makes the checkbox mutually exclusive for the whole column, i.e. if I click on row 1, I cannot click on row 2 or cleans the row 1. This is great, but I need to accommodate across the columns, not the rows.
Any help will be greatly appreciated.
Thanks a lot!
Tammy
Code:
Private Sub DataGridView1_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
' make the checkboxes mutually exclusive
If TypeOf DataGridView1.Columns(e.ColumnIndex) Is DataGridViewCheckBoxColumn Then
Dim state As Boolean = Convert.ToBoolean(DirectCast(DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex), DataGridViewCheckBoxCell).EditingCellFormattedValue)
Dim intDemarcationSetID As Integer = Convert.ToInt32(DirectCast(DataGridView1.Rows(e.RowIndex).Cells(0), DataGridViewTextBoxCell).EditedFormattedValue)
If state Then
' unselect all the other ones
For Each dgvr As DataGridViewRow In DataGridView1.Rows
If Convert.ToInt32(dgvr.Cells(0).Value) <> intDemarcationSetID Then
DirectCast(dgvr.Cells(e.ColumnIndex), DataGridViewCheckBoxCell).Value = False
End If
Next
End If
End If
End Sub
Last edited by jtammyg; Dec 12th, 2015 at 07:12 PM.
Tags for this Thread
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
|