将表单1上的数据网格中的项目发送到表单2上的另一个数据网格

时间:2017-04-10 17:11:29

标签: vb.net datagridview

我有两个datagridview的DGV1,它在表单1中,在表单1上,当单击该按钮时,我还有一个“下一步”按钮我希望DGV1中的项目集合自动转移到Form2中的DGV2。 任何帮助都会很棒 感谢

1 个答案:

答案 0 :(得分:0)

扩展Plutonix的建议,你想要做的事情是:

Private Sub btn_Next_Click(sender As Object, e As EventArgs) Handles btn_Next.Click
    If DGV1.DataSource IsNot Nothing Then    
        Me.Hide()
        Dim frmForm2 As New Form2
        Dim dgv2 As DataGridView
        dgv2 = frmForm2.Controls("DGV2")  
        dgv2.DataSource = DGV1.DataSource
        frmForm2.Show()
    End if
End Sub

或者,如果Form2基本上只有DataGridView,您可以重新使用整个现有的DataGridView。或者只是将Form1中的一个放入Form2(删除Designer中的那个,但将其属性复制到下面的代码中):

Private Sub btn_Next_Click(sender As Object, e As EventArgs) Handles btn_Next.Click
    If DGV1.DataSource IsNot Nothing Then
        Dim f As New Form
        f.Size = New Size(400, 300)
        Dim dgv As DataGridView = DGV1
        dgv.Dock = DockStyle.Fill
        ' ... plus any other customized properties
        f.Controls.Add(dgv)
        f.Text = "Form2"
        f.Show()
        f.Activate()
    End If
End Sub

编辑:我现在已经测试过第一个代码块可以正常运行,而无需更改任何公共/私有设置。