lundi 31 juillet 2023

How checkbox multi datagridview from form2 to form1 di vb.net

How checkbox multi datagridview from form2 to form1 di vb.net ?

If I do with a single checkbox for each then the result is appropriate but if I do a multi checkbox at once then the result does not match as below screenshot is there anything wrong with my code .

Please Guide me

Thanks

CODE IN FORM1

Public Class Form1
    Private table1 As New DataTable
 Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        table1.Columns.Add(New DataColumn("Column1", GetType(String)))
        table1.Columns.Add(New DataColumn("Column2", GetType(String)))
        table1.Columns.Add(New DataColumn("Column3", GetType(String)))
        table1.Columns.Add(New DataColumn("Column4", GetType(String)))
        table1.Columns.Add(New DataColumn("Qty", GetType(Integer)))
        DataGridView1.DataSource = table1
    End Sub
  Private Sub BTNAdd_Click(sender As Object, e As EventArgs) Handles BTNAdd.Click
        Dim Column2 As String = TextBox2.Text.Trim()
        Dim Column1 = TextBox1.Text.Trim()
        Using frm = New Form2(Column2)

            If frm.ShowDialog() = DialogResult.OK Then
                Dim datarow As DataRow = Nothing
                Dim Column3 = frm.DataGridView1.CurrentRow.Cells(3).Value?.ToString()
                Dim Column4 = frm.DataGridView1.CurrentRow.Cells(4).Value?.ToString()
                If table1.Rows.Cast(Of DataRow).Any() Then
  datarow = table1.Select($"Column1 = '{Column1}' AND Column2 = '{Column2}' AND Column3 = '{Column3}' AND Column4 = '{Column4}'").FirstOrDefault()
                End If
                If datarow IsNot Nothing Then
                    Dim qty = If(datarow("Qty") Is Nothing, 0, datarow.Field(Of Integer)("Qty"))
                    datarow.SetField("Qty", qty + 1)
                    Return
                Else
                End If
                Try
                    For Each row2 As DataGridViewRow In frm.DataGridView1.Rows
                        Dim isselect As Boolean = Convert.ToBoolean(row2.Cells("checkboxcolumn").Value)
                        If isselect Then
                            table1.Rows.Add(Column1, Column2, Column3, Column4, 1)
                            TextBox1.Clear()
                            TextBox2.Clear()
                        End If
                    Next
                Catch ex As Exception
                    MessageBox.Show(ex.Message)
                End Try
            Else

                Return
            End If
        End Using
    End Sub
End Class

CODE IN FORM2

Public Class Form2
    Sub New()
        InitializeComponent()
    End Sub
    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

    Public Sub New(Column2 As String)
        Me.New
        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 Table2)(table2)
        Dim CheckedBoxColumn As New DataGridViewCheckBoxColumn
        CheckedBoxColumn.Width = 40
        CheckedBoxColumn.Name = "checkboxcolumn"
        CheckedBoxColumn.HeaderText = "Check"
        DataGridView1.Columns.Insert(0, CheckedBoxColumn)
    End Sub

    Private Sub BtnOK_Click(sender As Object, e As EventArgs) Handles BtnOK.Click
        DialogResult = DialogResult.OK
    End Sub
End Class
Public Class Table2

    Public Property Column1() As String
    Public Property Column2() As String
    Public Property Column3() As String
    Public Property Column4() As String

End Class

the result does not match

RESULT IN DATAGRIDVEW FORM1

DESIRED RESULT

Column1 Column2 Column3 Column4
001 TEST1 TEST1 TEST1
001 TEST1 TEST1
001 TEST1
001 TEST1 TEST1
002 TEST2 TEST2



Aucun commentaire:

Enregistrer un commentaire