Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
For Each bos As DataGridViewRow In DataGridView1.Rows
DataGridView2.Rows.Add()
For Each row As DataGridViewColumn In DataGridView2.Columns
DataGridView1.Rows(0).Cells(row.Index).Value = DataGridView2.Rows(bos.Index).Cells(row.Index).Value.ToString
Next
Next
End Sub
Dim rc As Integer = Me.DataGridView2.Rows.Count
Me.DataGridView2.Rows.Add()
Me.DataGridView2.Rows(rc).Cells(0).Value = Me.DataGridView1.CurrentRow.Cells(0).Value
Me.DataGridView2.Rows(rc).Cells(1).Value = Me.DataGridView1.CurrentRow.Cells(1).Value
แอดตารางเดียว โดยตรวจว่ามันมีตารางซ้ำหรือไม่
Code (VB.NET)
For r As Integer = 0 To Me.DataGridView2.Rows.Count - 1
If Me.DataGridView1.CurrentRow.Cells(0).Value = Me.DataGridView2.Rows(r).Cells(0).Value Then
Exit For
End If
Dim rc As Integer = Me.DataGridView2.Rows.Count
Me.DataGridView2.Rows.Add()
Me.DataGridView2.Rows(rc).Cells(0).Value = Me.DataGridView1.CurrentRow.Cells(0).Value
Me.DataGridView2.Rows(rc).Cells(1).Value = Me.DataGridView1.CurrentRow.Cells(1).Value
Next
แอดหลายตารางโดยไม่ตรวจว่ามันซ้ำหรือไม่
Code (VB.NET)
For Each row As DataGridViewRow In DataGridView1.Rows
If row.Selected = True Then
Dim rc As Integer = Me.DataGridView2.Rows.Count
Me.DataGridView2.Rows.Add()
Me.DataGridView2.Rows(rc).Cells(0).Value = row.Cells(0).Value
Me.DataGridView2.Rows(rc).Cells(1).Value = row.Cells(1).Value
End If
Next
แอดหลายตาราง โดยตรวจว่ามันซ้ำกันหรือไม่
Code (VB.NET)
For Each row As DataGridViewRow In DataGridView1.Rows
If row.Selected = True Then
For Each row2 As DataGridViewRow In DataGridView2.Rows
If row.Cells(0).Value = row2.Cells(0).Value Then
GoTo GotoNoAddRow
End If
Next row2
Dim rc As Integer = Me.DataGridView2.Rows.Count
Me.DataGridView2.Rows.Add()
Me.DataGridView2.Rows(rc).Cells(0).Value = row.Cells(0).Value
Me.DataGridView2.Rows(rc).Cells(1).Value = row.Cells(1).Value
GotoNoAddRow:
End If
Next
For iRow = 0 To DataGridView1.Rows.Count - 1
Dim row As String() = New String() {DataGridView1.Rows(iRow).Cells(0).Value, DataGridView1.Rows(iRow).Cells(1).Value, DataGridView1.Rows(iRow).Cells(2).Value}
DataGridView2.Rows.Add(row)
Next