0

How to Copy Contents in one DataGridview to another DataGridview that in another form

For i As Integer = DataGridView1.Rows.Count - 1 To 0 Step -1
    With DataGridView1.Rows(i)
        DataGridView2.Rows.Insert(0, .Cells(0).Value, .Cells(1).Value, .Cells(2).Value, .Cells(3).Value)
    End With
Next
djv
  • 15,168
  • 7
  • 48
  • 72
  • 1
    What are the issue's you are experiencing? Also are you filling the first `DataGridView` from a source, if so you don't need to manually add rows from the first to the second. There are many ways this can be handled, but currently the post lacks information, please update so we can help you further. – Trevor Apr 15 '19 at 15:16
  • i read a textfile to datagrid and test if that data is exist in database or not after testing i want to copy it to another form wich have a datagridview where i added others columns to update the database from it, the issue i have no idea how it will grab rows from form and puched to other datagridview in form1 – Mahjoubi houssem Apr 15 '19 at 15:21
  • Does your code above throw any exception? or what do you expect to get and are not getting? – preciousbetine Apr 15 '19 at 15:23
  • IMHO you should have a class to work with, look into creating classes to hold your data from the text file you read. This data then can be displayed/used/shared/updated anywhere you need it. For starters, we don't know what that text file looks like, how are you checking the DB to see if that data exist...? There's a lot of uncertainty here we don't know about to form a good answer to help you. Of course, we could throw an answer out, but would *that actually address your issue?* – Trevor Apr 15 '19 at 15:23
  • " i have no idea how it will grab rows from form and puched to other datagridview in form1" You need a **reference** to the other form. Which forms are involved? Which forms shows which? Details, my friend... – Idle_Mind Apr 15 '19 at 15:26
  • my code copy data from datagrid to another in same form but i want to other datagrid in other form,so i have no idea how it work,only i have a command to get value from datagrid and i can passed to another form("DataGridView1.CurrentRow.Cells(0).Value.ToString") – Mahjoubi houssem Apr 15 '19 at 15:30

1 Answers1

0

See this answer which already covers copying from one DataGridView to another.

I took the code and converted to vb.net using an online converter.

For this code to work, make a new project with two forms (Form1, Form2), each with a DataGridView on them (both named DataGridView1). Place a button on Form1 (Button1). Then paste the code in each form's respective code file.

Form1

Public Class Form1
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        DataGridView1.DataSource = {"A", "B", "C"}.Select(Function(s) New With {.Value = s}).ToList()
        Form2.Show()
    End Sub
    Public Sub Foo()
        Form2.Bar(DataGridView1)
    End Sub
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Foo()
    End Sub
End Class

Form2

Public Class Form2
    Public Sub Bar(dgv_org As DataGridView)
        Dim dgv_copy = DataGridView1
        Try
            If dgv_copy.Columns.Count = 0 Then

                For Each dgvc As DataGridViewColumn In dgv_org.Columns
                    dgv_copy.Columns.Add(TryCast(dgvc.Clone(), DataGridViewColumn))
                Next
            End If
            Dim row As DataGridViewRow = New DataGridViewRow()
            For i As Integer = 0 To dgv_org.Rows.Count - 1
                row = CType(dgv_org.Rows(i).Clone(), DataGridViewRow)
                Dim intColIndex As Integer = 0
                For Each cell As DataGridViewCell In dgv_org.Rows(i).Cells
                    row.Cells(intColIndex).Value = cell.Value
                    intColIndex += 1
                Next
                dgv_copy.Rows.Add(row)
            Next
            dgv_copy.AllowUserToAddRows = False
            dgv_copy.Refresh()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub
End Class
djv
  • 15,168
  • 7
  • 48
  • 72
  • So you are posting the answer from another answer instead of voting to close as a duplicate per say, I am confused... – Trevor Apr 15 '19 at 15:44
  • I'm sorry about that... were you confused about the term "duplicate" or what? – djv Apr 15 '19 at 16:03
  • No, I am saying why would you copy an answer to translate for the OP, instead of voting to close and or just providing a link in a comment? Why do we need this code *again* in another place, no need to apologize :) – Trevor Apr 15 '19 at 16:05
  • Because it is not a duplicate. OP is asking how to copy from one form's dgv to another form's dgv. The linked answer only deals with dgv to dgv. This answer as you can see is very different from the linked one. – djv Apr 15 '19 at 16:06
  • Understand, but you are dealing with `DataGridView`'s either way correct? The real issue is going from `DataGridView` to `DataGridView`...right? OP said in comment `my code copy data from datagrid to another in same form`.. I guess there are just as confused? – Trevor Apr 15 '19 at 16:11
  • I wrote this before any of the comments. What's another -2 rep? – djv Apr 15 '19 at 16:16
  • i want put a datatable in place of {"A", "B", "C"} and does not work DataGridView1.DataSource = {"A", "B", "C"}.Select(Function(s) New With {.Value = s}).ToList() – Mahjoubi houssem Apr 15 '19 at 16:20
  • djv your code answer exactly what i want but i have hard time understanding it,i need now as i said to replace {"A", "B", "C"} with datatable – Mahjoubi houssem Apr 15 '19 at 16:24
  • I didn't see how you got the data into the first datagridview, so I put my own data in for testing. Just do it how you were doing it before me. – djv Apr 15 '19 at 16:25
  • ok thanks a lot i get data from textfile like this ` Dim ouvrir As New OpenFileDialog If ouvrir.ShowDialog = DialogResult.OK Then Dim sr As New StreamReader(ouvrir.FileName) While Not sr.EndOfStream dt2.Rows.Add(sr.ReadLine.Split(CChar(vbTab))) End While End If` – Mahjoubi houssem Apr 16 '19 at 13:06