Athirah Hazira
Athirah Hazira

Reputation: 473

Pass all rows from a DataGridView on one form to another vb.net

So i am trying to pass all rows from a DataGridView on a Button Click from one form to another. Which is from grd_order in frm_1.vb to grd_invoice in frm_2.vb

This is as far as i can go since i am stil new to 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

Upvotes: 1

Views: 1806

Answers (2)

akhil kumar
akhil kumar

Reputation: 1618

try this

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

hope this helps.

Upvotes: 1

Reza Aghaei
Reza Aghaei

Reputation: 125207

The most simple way would be passing the DataSource of your first DataGridView to second one.

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

You can also define the Data property as DataTable and pass the data table that you have on your first form, to the second form.

Upvotes: 1

Related Questions