Reputation: 5056
I have a DataGridView
, said dgA
. This contains some informations that I need to copy in another DataGridView
said dgB
as soon buttons btn
is clicked.
How can I do this in Visual Basic?
Upvotes: 0
Views: 4403
Reputation: 4938
Why not go through each row of your DataGridView1
(dgA) and send the cell values to DataGridView2
(dgB)?
I added two columns to my DataGridViews, so apply this code respectively to your datagridview columns.
Private Sub CopyDgv1ToDgv2_Click(sender As System.Object, e As System.EventArgs) Handles CopyDgv1ToDgv2.Click
For Each r As DataGridViewRow In dgA.Rows
If r.IsNewRow Then Continue For
'r.Cells(0).Value is the current row's first column, r.Cells(1).Value is the second column
dgB.Rows.Add({r.Cells(0).Value, r.Cells(1).Value})
Next
End Sub
This goes through each row of my first DataGridView
and adds a row in my second DataGridView
with the values contained in the first DataGridView
.
If the data is bound on both DataGridViews
then all you need to do is copy the data source to the other DataGridView like so:
Private Sub CopyDgv1ToDgv2_Click(sender As System.Object, e As System.EventArgs) Handles CopyDgv1ToDgv2.Click
dgB.DataSource = dgA.DataSource
End Sub
Upvotes: 1
Reputation: 4726
You can copy the data source of dgA (Being a DataTable for example), and bind dgB to it aswell. You should get the same data source on the 2 grids.
Example:
Dim dtSource As New DataTable
' Configure your source here...
' Bind first grid
dgA.DataSource = dtSource
dgA.DataBind()
' Use same data source for this grid...
dgB.DataSource = dgA.DataSource
dgB.DataBind()
You can then alter the configuration of how the grid appears from the .ASPX. You can also use a session, to be used in different pages.
Upvotes: 1