Hi,

I just need a little help here, I wanted to copy the rows in current form's datagridview to another form's datagridview.

Here is my example rows in datagridview1 in current row

ID | ProID | Name
1  | 1     | Shirt
2  | 1     | Shoes
3  | 2     | Pants
4  | 2     | Cap

What I want is when I selected shirt and click the button all rows with the same ProID as the selected row will be copied to another form's datagridview.

Here is my code but it only transfer the selected row and not all rows with the same ProID as the selected row. I don't have an idea on how to loop all rows to be copied.


Dim i As Integer = Promotion.DataGridView1.Rows.Count + 1
            Promotion.DataGridView1.Rows.Add(i, DataGridView1.CurrentRow.Cells("PLU").Value, DataGridView1.CurrentRow.Cells("Barcode").Value, DataGridView1.CurrentRow.Cells("ItemName").Value, DataGridView1.CurrentRow.Cells("ItemPrice").Value, DataGridView1.CurrentRow.Cells("Description").Value)

Dani AI

Generated

Brief summary and a robust pattern to solve this reliably (bound vs unbound)

The core issue is that a data‑bound DataGridView should normally be manipulated through its underlying data source rather than by adding rows directly to the grid. As later found, persisting the selected items via the database is one valid approach; ’s comment about looping is useful when the grid is unbound. The recommended, clearer patterns are: (A) filter or query the bound DataTable/BindingSource for the matching ProID and bind the result to the target grid, or (B) copy DataRow objects from the source DataTable into a destination DataTable and bind that to the other form.

Example (VB.NET) — copy matching rows from a bound source DataTable into the destination DataTable used by the second form:

' get selected ProID safely
Dim sel = DataGridView1.CurrentRow
If sel Is Nothing Then Return
Dim proID As Integer
If Not Integer.TryParse(Convert.ToString(sel.Cells("ProID").Value), proID) Then Return

Dim src As DataTable = TryCast(DataGridView1.DataSource, DataTable)
If src Is Nothing Then Return

Dim matches() As DataRow = src.Select("ProID = " & proID)
If matches.Length = 0 Then Return

Dim dst As DataTable = TryCast(Promotion.DataGridView1.DataSource, DataTable)
If dst Is Nothing Then
  dst = src.Clone()    ' same schema
  Promotion.DataGridView1.DataSource = dst
End If

For Each r As DataRow In matches
  dst.ImportRow(r)    ' or dst.Rows.Add(r.ItemArray) if keys/schema differ
Next

Practical notes and cautions

  • If the destination table has a primary key, duplicate keys will raise exceptions; use Clone/Adjust schema or remove/modify keys as needed.
  • BindingSource.Filter or DataView.RowFilter is simpler when the intention is to show “all rows with ProID = X” rather than physically copying rows.
  • Prefer passing a DataTable/BindingList or a public method on the Promotion form (e.g., AddRows(dt)) instead of directly accessing controls across forms — it keeps code decoupled and testable.
  • Handle DBNull and parsing robustly, and check for null CurrentRow before reading cells.

Recommended Answers

All 4 Replies

I wonder if your code would ever compile and it certainly does not use two datagridviews.
Here is an example on how to loop through datagridviewrows

Actualy it compiled and by the way I already solved my problem, the 1st datagridview was data bounded so I just decided to get the values to be transfered in 2nd datagridview in database instead of the 1st datagridview... Anyway thank you so much for the reply.. I just wonder why it tooks so many hours before we get a reply from this website knowing that a lot of professional programmers are navigating this forum.

Perhaps many have a weekend? Or are on holyday? Perhaps some have other things to do? We are all here on a free basis you know. We are certainly not paid for the services. I would even refuse to get paid for it.

Can I hire you to code POS Hardware for our project? If you're interested please add me in facebook. fb.com/yvrej17

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.