Vb.net 如何复制DataGridView';把内容放在另一个里面

Vb.net 如何复制DataGridView';把内容放在另一个里面,vb.net,datagridview,Vb.net,Datagridview,我有一个DataGridView,上面写着dgA。这包含一些信息,我需要在另一个DataGridViewsaiddgB中复制这些信息,只要单击按钮btn 如何在Visual Basic中执行此操作?您可以复制dgA的数据源(例如数据表),并将dgB绑定到该数据源。您应该在两个网格上获得相同的数据源 例如: Dim dtSource As New DataTable ' Configure your source here... ' Bind first grid dgA.DataSou

我有一个
DataGridView
,上面写着
dgA
。这包含一些信息,我需要在另一个
DataGridView
said
dgB
中复制这些信息,只要单击按钮
btn


如何在Visual Basic中执行此操作?

您可以复制dgA的数据源(例如数据表),并将dgB绑定到该数据源。您应该在两个网格上获得相同的数据源

例如:

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()
然后,您可以更改.ASPX中网格显示方式的配置。您还可以使用会话,在不同页面中使用。

非数据绑定DataGridView 为什么不检查一下
DataGridView1
(dgA)的每一行,并将单元格值发送到
DataGridView2
(dgB)

我在datagridview中添加了两列,因此将此代码分别应用于datagridview列

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
这将遍历第一个
DataGridView
的每一行,并在第二个
DataGridView
中添加一行,其中包含第一个
DataGridView
中包含的值


DataBoundDataGridView 如果数据绑定在两个
DataGridView
上,则只需将数据源复制到另一个DataGridView,如下所示:

Private Sub CopyDgv1ToDgv2_Click(sender As System.Object, e As System.EventArgs) Handles CopyDgv1ToDgv2.Click
    dgB.DataSource = dgA.DataSource
End Sub

两个DataGridView中的列是否相同?DataGridView是否绑定到数据源?是,完全相同。DataGrid绑定到一个数据源,但它是另一个子例程的内部,我不会将其设置为全局。DataGrid是另一个子例程的内部,我不会将其设置为全局。可能将其存储在会话中?i、 e您获得一个数据源,将其存储在一个会话中,该会话将用于另一个网格?显然假设dgA先加载我明白了。。。我认为只有第一个DataGridView绑定到数据源。在这种情况下,两者都必须受到约束。