How to restrict users to check only a single checkbox in a DataGridView

62 Views Asked by At

How can implement such a constraint? Please guide

Thanks

Public Class Form4
    Public Function ConvertToList(Of T)(ByVal dt As DataTable) As List(Of T)
        Dim columnNames = dt.Columns.Cast(Of DataColumn)().Select(Function(c) c.ColumnName).ToList()
        Dim properties = GetType(T).GetProperties()
        Return dt.AsEnumerable().Select(
            Function(row)
                Dim objT = Activator.CreateInstance(Of T)()
                For Each pro In properties
                    If columnNames.Contains(pro.Name) Then
                        Dim pI As PropertyInfo = objT.GetType().GetProperty(pro.Name)
                        pro.SetValue(objT, If(row(pro.Name) Is DBNull.Value, Nothing, Convert.ChangeType(row(pro.Name), pI.PropertyType)))
                    End If
                Next pro
                Return objT
            End Function).ToList()
    End Function

    Private Sub Form4_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim table2 As New DataTable("Players")
        table2.Columns.Add(New DataColumn("Column1", GetType(String)))
        table2.Columns.Add(New DataColumn("Column2", GetType(String)))
        table2.Columns.Add(New DataColumn("Column3", GetType(String)))
        table2.Columns.Add(New DataColumn("Column4", GetType(String)))
        table2.Rows.Add("001", "TEST1", "TEST1", "TEST1")
        table2.Rows.Add("001", "TEST1", "TEST1", "")
        table2.Rows.Add("001", "TEST1", "", "")
        table2.Rows.Add("001", "TEST1", "", "TEST1")
        table2.Rows.Add("002", "TEST2", "", "TEST2")
        DataGridView1.DataSource = ConvertToList(Of Tabletwo)(table2)
        Dim CheckedBoxColumn As New DataGridViewCheckBoxColumn
        CheckedBoxColumn.Width = 40
        CheckedBoxColumn.Name = "checkboxcolumn"
        CheckedBoxColumn.HeaderText = "Check"
        DataGridView1.Columns.Insert(0, CheckedBoxColumn)
    End Sub
End Class

Public Class Tabletwo
    Public Property Column1() As String
    Public Property Column2() As String
    Public Property Column3() As String
    Public Property Column4() As String
End Class
1

There are 1 best solutions below

0
roy On

According to the recommendation link from @dr.null

Visit Make only (2) checkboxes in a datagridview column checkable at a time

Private Sub DataGridView1_CurrentCellDirtyStateChanged(sender As Object, e As EventArgs) Handles DataGridView1.CurrentCellDirtyStateChanged

    If TypeOf DataGridView1.CurrentCell Is DataGridViewCheckBoxCell AndAlso DataGridView1.IsCurrentCellDirty AndAlso Not CBool(DataGridView1.CurrentCell.FormattedValue) Then
        Dim count = DataGridView1.Rows.Cast(Of DataGridViewRow)().SelectMany(Function(r) r.Cells.OfType(Of DataGridViewCheckBoxCell)().Where(Function(c) CBool(c.FormattedValue))).Count()

        If count = 1 Then
            MessageBox.Show("User Only can checklist one and please uncheck other")
            DataGridView1.CancelEdit()
        End If
    End If

End Sub