Click here to Skip to main content
15,881,644 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i want to copy rows from one datagridview in form1 to another datagridview in form2
Posted

1 solution

The first thing you need is the instance of Form2: the one that is being displayed.
Somewhere, you have created it, and displayed it, either using Show:
VB
Dim f2 As New Form2()
f2.Show()
Or you are going to use ShowDialog:
VB
Dim f2 As New Form2()
f2.ShowDialog()
In either case, you need the f2 variable available when you want to do the copy. In the second case, that's probably trivial because you will be setting the values between the create and display of the form.

Add a Public method to your Form2:
VB
Public Sub AddRow(row As DataGridViewRow)
	myDataGridView.Rows.Add(row)
End Sub
Then call that in a loop from Form1:
VB
Dim f2 As New Form2()
For Each row As DataGridViewRow In mySourceDataGridView.Rows
	f2.AddRow(row)
Next
f2.ShowDialog()
 
Share this answer
 
v2
Comments
[no name] 8-Aug-15 4:58am    
that didn't work ,
OriginalGriff 8-Aug-15 5:05am    
"that didn't work" is not a helpful error report. It tells me nothing.

So what happened when you tried? Anything? Any error message? Effects?
I can't see your screen, access your HDD, or read your mind! :laugh:
[no name] 8-Aug-15 5:01am    
please help me
[no name] 8-Aug-15 5:01am    
can you write full code for me !! ?
[no name] 8-Aug-15 5:14am    
this is the error : Unable to cast object of type 'System.Data.DataRowView' to type 'System.Windows.Forms.DataGridViewRow'.

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