Click here to Skip to main content
15,041,788 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear All,

With below code I cam copy all the Column names but can not copy the Rows. Am I missing Something?

VB
Dim x As Integer

        For x = 0 To DataGridView1.Columns.Count - 1
            DataGridView2.Columns.Add(DataGridView1.Columns(x).Clone())
        Next

        MsgBox("clm added:")

        For x = 0 To DataGridView1.Rows.Count - 1
            DataGridView2.Rows.Add(DataGridView1.Rows(x).Clone())
        Next


Please help
Posted
Comments
ZurdoDev 5-Jan-15 13:17pm
   
When you debug it, what is DataGridView1.Rows.Count?

1 solution

you need to copy the cell values, sample code:
VB
If dgv_copy.Columns.Count = 0 Then
	For Each dgvc As DataGridViewColumn In dgv_org.Columns
		dgv_copy.Columns.Add(TryCast(dgvc.Clone(), DataGridViewColumn))
	Next
End If

Dim row As New DataGridViewRow()

For i As Integer = 0 To dgv_org.Rows.Count - 1
	row = DirectCast(dgv_org.Rows(i).Clone(), DataGridViewRow)
	Dim intColIndex As Integer = 0
	For Each cell As DataGridViewCell In dgv_org.Rows(i).Cells
		row.Cells(intColIndex).Value = cell.Value
		intColIndex += 1
	Next
	dgv_copy.Rows.Add(row)
Next

refer : Copying a DataGridViewRow to another DataGridView Control[^]
How To DataGridView Rows Copy To Another On CheckBoxColumns Checked In VB.NET[^]
   
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900