i want to copy rows from one datagridview in form1 to another datagridview in form2
please help me !!
Hardz 15 Light Poster
Hi,
What have you done so far? Is your dgv a databound or not? Anyway, please see this link on passing value from dgv to another.
Hardz
zelrick -1 Junior Poster
Hi Sir teze;
Addition Information:
I suggest use RowHeaderMouseDoubleClick on the event of DGV
Note: doubleclick the row of DGV1, it transfer the whole row to DGV2
Private Sub dgvEF_RowHeaderMouseDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles dgvEF.RowHeaderMouseDoubleClick
For rowIndex As Integer = 0 To (Me.ReadDataDataGridView.Rows.Count - 1)
If (Not Me.ReadDataDataGridView.Rows(rowIndex).IsNewRow) Then
Dim values As Object() = New Object(Me.ReadDataDataGridView.Columns.Count - 1) {}
For columnIndex As Integer = 0 To (Me.ReadDataDataGridView.Columns.Count - 1)
values(columnIndex) = Me.ReadDataDataGridView.Rows(rowIndex).Cells(columnIndex).Value
Next
Me.DataGridView1.Rows.Add(values)
End If
Next
End Sub
Hardz 15 Light Poster
@zelrick: Thanks for the code that you have provided.
@teze: Hi, How is it going so far? Do you still having trouble loading dgv value to a dgv to other form? Anyway, I have here another sample on how to load dgv to dgv to another form.
@ Form1:
Dim dt As DataTable
'Handle form load event to initialize dgv
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
dt = New DataTable()
'using 3 columns in a table
dt.Columns.Add("ColA")
dt.Columns.Add("ColB")
dt.Columns.Add("ColC")
DataGridView1.DataSource = dt
'loading sample data to a dgv which is consists of 5 rows
For i As Integer = 1 To 5
dt.Rows.Add("A" & i, "B" & i, "C" & i)
Next
End Sub
Dim f2 As Form2
'There have been many options on how to load the dgv to another dgv:
'1. Load directly the datatable value of dgv1 to dgv2 through its datasource. By handling button1 click event:
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
f2 = New Form2()
'calling the function to load datatable directly to a dgv datasource of form2.
f2.loadDgv1ToDgv2Method1(dt)
f2.ShowDialog()
End Sub
'2. Or this method that uses loop to load the dgv1 datarows to dgv2, which is I think a decent one. So by handling another button click event:
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
f2 = New Form2()
'initializing dgv at form2
f2.initializeDgvAtFrm2()
'loading dgv datarows to a datatable of dgv2.
For Each row As DataRow In dt.Rows
f2.loadDgv1ToDgv2Method2(row("ColA").ToString(), row("ColB").ToString(), row("ColC").ToString())
Next
f2.ShowDialog()
End Sub
Then at Form2:
'1st method:
Public Sub loadDgv1ToDgv2Method1(dt1 As DataTable)
DataGridView1.DataSource = dt1
End Sub
'2nd method:
'Initializing dgv Columns, assuming that dgv columns of form1 is the same with form2 dgv columns.
Dim dt As DataTable
Public Sub initializeDgvAtFrm2()
dt = New DataTable()
'using 3 columns in a table
dt.Columns.Add("ColA")
dt.Columns.Add("ColB")
dt.Columns.Add("ColC")
DataGridView1.DataSource = dt
End Sub
Public Sub loadDgv1ToDgv2Method2(col1 As String, col2 As String, col3 As String)
dt.Rows.Add(col1, col2, col3)
End Sub
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.