将所有行从一个表单上的DataGridView传递到另一个表单

时间:2016-01-07 08:20:21

标签: .net vb.net winforms datagridview

所以我试图将按钮单击上的DataGridView中的所有行从一个表单传递到另一个表单。这是从frm_1.vb中的grd_order到frm_2.vb中的grd_invoice

这是我可以去的,因为我不知道vb.net

frm_1.vb

Private Sub btn_purchase_Click(sender As System.Object, e As System.EventArgs) Handles btn_purchase.Click
      Dim newForm As New frm_2
      newForm.AllRows = grd_order.DataSource
      'what should i put here because the above line is wrong
      newForm.Show()
End Sub

frm_2.vb

Public Class frm_2
      Public Property AllRows As DataGridViewSelectedRowCollection
      'I know that DataGridViewSelectedRowCollection is used for the specific row but what about all rows?

      Private Sub frm_2_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
      'grd_invoice.DataSource = PassText
      'what should i put here
      End Sub
End Class

2 个答案:

答案 0 :(得分:1)

试试这个

Private Sub btn_purchase_Click(sender As System.Object, e As System.EventArgs) Handles btn_purchase.Click
      Dim newForm As New frm_2
      For Each col As DataGridViewColumn In grd_order.Columns
        newform.grd_invoice.Columns.Add(col)
      Next
      For Each row as DataGridViewRow in grd_order.rows

       newForm.grd_invoice.Rows.Add(row)

      next
     newForm.Show()
End Sub

希望这会有所帮助。

答案 1 :(得分:1)

最简单的方法是将第一个DataSource的{​​{1}}传递给第二个。

DataGridView

您还可以将Public Class frm_customer_invoice_a153834 Public Property Data As Object Private Sub Form_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load Me.grd_invoice.DataSource = Data End Sub End Class 属性定义为Data,并将您在第一个表单上的数据表传递给第二个表单。