0

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
Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188
roy
  • 693
  • 2
  • 11
  • 2
    When you look at your code a few months later, will you still know what `Form4`, `Tabletwo`, `Button3` and `TextBox17` are? Take your time to give your types, controls and variables better names. – Olivier Jacot-Descombes Aug 01 '23 at 14:25
  • @OlivierJacot-Descombes , `When you look at your code a few months later, will you still know what Form4, Tabletwo, Button3 and TextBox17 are?` Form4 is only for testing, which is in form4 and Tabletwo is this list model class. Button3 and TextBox17 for this I no post – roy Aug 01 '23 at 14:31
  • Alternatively, you could set `DataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect` and `DataGridView1. MultiSelect = False`. – Olivier Jacot-Descombes Aug 01 '23 at 14:32
  • 1
    https://stackoverflow.com/a/73649966/14171304 – dr.null Aug 01 '23 at 14:38
  • 1
    @dr.null , Thank you recommended link from you – roy Aug 01 '23 at 14:58
  • @dr.null , I have another link maybe you can help [link](https://stackoverflow.com/questions/76804108/how-to-sort-3-columns-of-datagridview-in-vb-net/76806392#76806392) – roy Aug 01 '23 at 15:03

1 Answers1

0

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
Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188
roy
  • 693
  • 2
  • 11