Click here to Skip to main content
14,384,739 members
Rate this:
Please Sign up or sign in to vote.
See more:
i want to copy rows from one datagridview in form1 to another datagridview in form2
Posted

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

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:
Dim f2 As New Form2()
f2.Show()
Or you are going to use ShowDialog:
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:
Public Sub AddRow(row As DataGridViewRow)
	myDataGridView.Rows.Add(row)
End Sub
Then call that in a loop from Form1:
Dim f2 As New Form2()
For Each row As DataGridViewRow In mySourceDataGridView.Rows
	f2.AddRow(row)
Next
f2.ShowDialog()
   
v2
Comments
Member 11894724 8-Aug-15 4:58am
   
that didn't work ,
Member 11894724 8-Aug-15 5:01am
   
please help me
Member 11894724 8-Aug-15 5:01am
   
can you write full code for me !! ?
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:
Member 11894724 8-Aug-15 5:14am
   
this is the error : Unable to cast object of type 'System.Data.DataRowView' to type 'System.Windows.Forms.DataGridViewRow'.
Member 11894724 8-Aug-15 5:18am
   
this is my code :

form1

Public Class Form1


Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'T1DataSet.Table1' table. You can move, or remove it, as needed.
Me.Table1TableAdapter.Fill(Me.T1DataSet.Table1)

End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim f2 As New Form2()
For Each row As DataGridViewRow In Me.Table1BindingSource
f2.AddRow(row)
Next
f2.ShowDialog()
End Sub
End Class

form2

Public Class Form2

Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'T2DataSet.Table1' table. You can move, or remove it, as needed.
Me.Table1TableAdapter.Fill(Me.T2DataSet.Table1)

End Sub
Public Sub AddRow(row As DataGridViewRow)
DataGridView1.Rows.Add(row)
End Sub
End Class
Member 11894724 8-Aug-15 5:37am
   
can you enter my pc ?
(teamviewr)
Member 11894724 8-Aug-15 5:45am
   
i think the problem in my binding source name

my source name is table1bindingsource , what i should write
OriginalGriff 8-Aug-15 6:00am
   
Why are you trying to use the binding source instead of the Rows collection?
Member 11894724 8-Aug-15 6:09am
   
my datagridview1 datasource name is : table1bindingsource1

tools :

button1 : copy
datagridview1
datagridview1

forms :
form1 - button : copy , datagridview1
form2 - datagridview2

can you help me , and write the code for me with my tools name !

please !

thank you bro

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100