none
How can I copy specific rows from datagradview1 to datagradview2 which is data bound? RRS feed

  • Question

  • How can I copy specific rows from datagradview1 to datagradview2 which is data bound? or what is code to can just copy specifi rows from dgv 1 to dgv 2 ......dgv2 have a date from sql and columns in dgv2 has a data property name

    Public Class test3
        Dim DTable1 As New DataTable
        Dim DTable2 As New DataTable
        Private Sub test3_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dim da As New SqlDataAdapter
            DTable1.Clear()
            da = New SqlDataAdapter("select cCDOE,cNAME from ITEMS", Con)
            da.Fill(DTable1)
            DataGridView1.DataSource = DTable1
            'DataGridView1.DataSource = DTable1.Clone
    
    
            Dim da1 As New SqlDataAdapter
            DTable2.Clear()
            da1 = New SqlDataAdapter("select qty,cost from ware", Con)
            da1.Fill(DTable2)
            DataGridView2.DataSource = DTable2
            'DataGridView2.DataSource = DTable2.Clone
    
        End Sub
    
        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    
            DTable2 = DTable1.Clone
            Dim dr As New System.Windows.Forms.DataGridViewRow
            For Each dr In Me.DataGridView1.SelectedRows
                Me.DataGridView2.Rows.Add(dr.Cells(0).Value, dr.Cells(1).Value)
    
            Next
    
        End Sub
    End Class



    Saturday, April 27, 2019 11:42 AM

All replies

  • Hi,
    try this:

      Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        For Each dr In Me.DataGridView1.SelectedRows
          Dim drv = TryCast(dr, DataGridViewRow)
          DTable2.Rows.Add(CType(drv.DataBoundItem, DataRowView).Row.ItemArray)
        Next
      End Sub


    --
    Best Regards / Viele Grüße
    Peter Fleischer (former MVP for Developer Technologies)
    Homepage, Tipps, Tricks

    Saturday, April 27, 2019 2:18 PM
  • thxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

    so much i want to kiss your head 


     your solve a big trouble




    • Edited by faysal13_13 Saturday, April 27, 2019 3:40 PM
    Saturday, April 27, 2019 3:30 PM
  • Hi,

    If your issue is solved, please Mark as answer to the appropriate answer , so that it will help other members to find solution quickly if they faces similar issue.

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, April 29, 2019 1:11 AM