1

can anyone please tell me how to merge two datagridviews in vb.net? I have following code but data doesn't align in one row. (see attached pdf).

Code:

Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click

    For Each col As DataGridViewColumn In DataGridView2.Columns
        DataGridView3.Columns.Add(DirectCast(col.Clone, DataGridViewColumn))

        For Each col2 As DataGridViewColumn In DataGridView1.Columns
            DataGridView3.Columns.Add(DirectCast(col2.Clone, DataGridViewColumn))
        Next
    Next

   For j As Integer = 0 To (DataGridView2.Rows.Count - 1)

       For k As Integer = 0 To (DataGridView1.Rows.Count - 1)
           DataGridView3.Rows.Add(DataGridView2.Rows(j).Cells.Cast(Of DataGridViewCell).Select(Function(c) c.Value).ToArray())
           DataGridView3.Rows.Add(DataGridView1.Rows(k).Cells.Cast(Of DataGridViewCell).Select(Function(c) c.Value).ToArray())
       Next
   Next

End Sub
T.S.
  • 18,195
  • 11
  • 58
  • 78
veeraka
  • 11
  • 2
  • 1
    I would recommend you merge the data source then bind it to the grid instead of manipulating grids, columns and rows directly. – Crowcoder Aug 31 '15 at 14:40
  • Since we already established that you should merge DataSource, not gridview display, here is working code already written http://stackoverflow.com/a/31480005/1704458 – T.S. Sep 01 '15 at 02:08

0 Answers0