Copy data from DataGrid View to another

ahmedAlie 161 Reputation points
2020-12-08T09:03:26.237+00:00

hi

I want to copy data from DataGrid View to another, as follows

46068-linkd.png

The second DataGrid View contains 8 columns, they are actually a duplicate of the first DataGrid, but in order for the data to be grouped next to some

46060-linkb.png

What do I want to collect data from the first DataGrid View by the second so that the columns that start inside the first column with the letter A in the first DataGrid View, which bear the symbol B in the second part of the DataGrid View 2, have the same name as it

46136-columng.png

And it becomes the final shape with the knowledge that the columns of the letter A can be more or vice versa, but the request is the same as collecting data next to some, even if some fields appear empty when the fields between A, B are not equal.

46070-grop.png

My Code

Blockquote

For ii = 0 To DATAG_CLIENT.Rows.Count - 1

           If DATAG_CLIENT.Rows(ii).Cells(0).Value.ToString.Contains("A") Then  
               PRINTSHOW.DATGR_PRINT.Rows(ii).Cells(0).Value = DATAG_CLIENT.Rows(ii).Cells(2).Value  
               PRINTSHOW.DATGR_PRINT.Rows(ii).Cells(1).Value = DATAG_CLIENT.Rows(ii).Cells(3).Value  
               PRINTSHOW.DATGR_PRINT.Rows(ii).Cells(2).Value = DATAG_CLIENT.Rows(ii).Cells(4).Value  
               PRINTSHOW.DATGR_PRINT.Rows(ii).Cells(3).Value = DATAG_CLIENT.Rows(ii).Cells(5).Value  

           End If  

           If DATAG_CLIENT.Rows(ii).Cells(0).Value.ToString.Contains("B") Then  


               PRINTSHOW.DATGR_PRINT.Rows(ii).Cells(4).Value = DATAG_CLIENT.Rows(ii).Cells(2).Value  
               PRINTSHOW.DATGR_PRINT.Rows(ii).Cells(5).Value = DATAG_CLIENT.Rows(ii).Cells(3).Value  
               PRINTSHOW.DATGR_PRINT.Rows(ii).Cells(6).Value = DATAG_CLIENT.Rows(ii).Cells(4).Value  
               PRINTSHOW.DATGR_PRINT.Rows(ii).Cells(7).Value = DATAG_CLIENT.Rows(ii).Cells(5).Value  

           End If  


       Next  

Blockquote

result show

46231-linkc.png

VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,642 questions
0 comments No comments
{count} votes

Accepted answer
  1. Viorel 114K Reputation points
    2020-12-08T09:29:26.523+00:00

    Try something like this:

    Dim a = 0
    Dim b = 0
    
    For ii = 0 To DATAG_CLIENT.Rows.Count - 1
       If DATAG_CLIENT.Rows(ii).Cells(0).Value.ToString.Contains("A") Then
          PRINTSHOW.DATGR_PRINT.Rows(a).Cells(0).Value = DATAG_CLIENT.Rows(ii).Cells(2).Value
          PRINTSHOW.DATGR_PRINT.Rows(a).Cells(1).Value = DATAG_CLIENT.Rows(ii).Cells(3).Value
          PRINTSHOW.DATGR_PRINT.Rows(a).Cells(2).Value = DATAG_CLIENT.Rows(ii).Cells(4).Value
          PRINTSHOW.DATGR_PRINT.Rows(a).Cells(3).Value = DATAG_CLIENT.Rows(ii).Cells(5).Value
          a += 1
       End If
    
       If DATAG_CLIENT.Rows(ii).Cells(0).Value.ToString.Contains("B") Then
          PRINTSHOW.DATGR_PRINT.Rows(b).Cells(4).Value = DATAG_CLIENT.Rows(ii).Cells(2).Value
          PRINTSHOW.DATGR_PRINT.Rows(b).Cells(5).Value = DATAG_CLIENT.Rows(ii).Cells(3).Value
          PRINTSHOW.DATGR_PRINT.Rows(b).Cells(6).Value = DATAG_CLIENT.Rows(ii).Cells(4).Value
          PRINTSHOW.DATGR_PRINT.Rows(b).Cells(7).Value = DATAG_CLIENT.Rows(ii).Cells(5).Value
          b += 1
       End If
    Next
    

    Also make sure that the destination rows (PRINTSHOW.DATGR_PRINT.Rows(a) and PRINTSHOW.DATGR_PRINT.Rows(b)) exist. You can use some PRINTSHOW.DATGR_PRINT.Rows.Add to add rows.

    Maybe consider StartsWith instead of Contains.

    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Xingyu Zhao-MSFT 5,356 Reputation points
    2020-12-09T07:07:20.1+00:00

    Hi @ahmedAlie ,

    The following code filter the DataGridView and set another DataGridView's datasource to a table merged by two tables.

        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load  
      
            Dim dt As DataTable = New DataTable()  
            dt.Columns.Add("No")  
            dt.Columns.Add("CarNumber")  
            dt.Columns.Add("Part")  
            dt.Columns.Add("Note")  
            dt.Rows.Add("A300", "C600", "KSJDA6", "SFF600")  
            dt.Rows.Add("A200", "C600", "KSJDA6", "SFF600")  
            dt.Rows.Add("A100", "C600", "SOIE", "EXCELLENT")  
            dt.Rows.Add("B300", "C600", "DOOR", "DRIT")  
            dt.Rows.Add("B200", "C600", "KSJDA6", "SFF600")  
            dt.Rows.Add("C600", "C600", "KSJDA6", "SFF600")  
            DataGridView1.DataSource = dt  
        End Sub  
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click  
            Dim dt = GetTableFromDGV(DataGridView1)  
            Dim tableA = dt.Select(String.Format($"No LIKE 'A%'")).CopyToDataTable()  
            Dim tableB = dt.Select(String.Format($"No LIKE 'B%'")).CopyToDataTable()  
      
            For Each col As DataColumn In tableB.Columns  
                col.ColumnName += "1"  
            Next  
            tableA.Merge(tableB.Clone())  
            For i As Integer = 0 To tableB.Rows.Count - 1  
                tableA.Rows(i)(4) = tableB.Rows(i)(0)  
                tableA.Rows(i)(5) = tableB.Rows(i)(1)  
                tableA.Rows(i)(6) = tableB.Rows(i)(2)  
                tableA.Rows(i)(7) = tableB.Rows(i)(3)  
            Next  
            DataGridView2.DataSource = tableA  
        End Sub  
        Private Function GetTableFromDGV(ByVal dgv As DataGridView) As DataTable  
            Dim dt As DataTable = New DataTable()  
            For Each col As DataGridViewColumn In dgv.Columns  
                dt.Columns.Add(col.Name)  
            Next  
            For Each row As DataGridViewRow In dgv.Rows  
                Dim dRow As DataRow = dt.NewRow()  
      
                For Each cell As DataGridViewCell In row.Cells  
                    dRow(cell.ColumnIndex) = cell.Value  
                Next  
                dt.Rows.Add(dRow)  
            Next  
            Return dt  
        End Function  
    

    Result of my test.
    46368-gif.gif

    Best Regards,
    Xingyu Zhao
    *
    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.