1 Replies - 683 Views - Last Post: 07 September 2012 - 04:47 PM Rate Topic: -----

#1 Keylogger  Icon User is offline

  • D.I.C Regular

Reputation: 4
  • View blog
  • Posts: 277
  • Joined: 14-February 11

DataGridView multiple rows

Posted 07 September 2012 - 02:53 PM

Hi there.
Imagine that I have one Form1.DataGridView1 which contains severals rows and several columns.
So, the user has the possibility of adding more rows or columns.

-------Column1 | Column2 | Column3
row1---cell1-----cell2-----cell3
row2---cell1-----cell2-----cell3
row3---cell1-----cell2-----cell3

Now, I need to pass all this information to other Form2.DataGridView1. This new DataGridView doesn't contain any kind of data, so I need to add the Columns first.

For col As Integer = 0 To Form1.DataGridView1.Columns.Count - 1
            Form2.DataGridView1.Columns.Add(col, "")
    Next


Ok, no problem at all.
However, now I need to get all the rows and the specific cells in each column.
I tried so far (without success):
For R As Integer = 0 To Form1.DataGridView1.Rows.Count - 1
            For C As Integer = 0 To Form1.DataGridView1.Columns.Count - 1
                Dim row As String() = New String() {Form1.DataGridView1.Rows(R).Cells(C).Value}
                Form2.DataGridView1.Rows.AddRange(row)
            Next
        Next


The problem is that instead of adding like this:
-------Column1 | Column2 | Column3
row1---cell1-----cell2-----cell3
row2---cell1-----cell2-----cell3
row3---cell1-----cell2-----cell3

It add like this:
-------Column1 | Column2 | Column3
row1---cell1-----
row2---cell2-----
row3---cell3-----
row4---cell1-----
row5---cell2-----
row6---cell3-----

I know the problem is in this line:
Dim row As String() = New String() {Form1.DataGridView1.Rows(R).Cells(C).Value}

But how can I solve it?

Is This A Good Question/Topic? 0
  • +

Replies To: DataGridView multiple rows

#2 CharlieMay  Icon User is online

  • This space intentionally left blank
  • member icon

Reputation: 1387
  • View blog
  • Posts: 4,466
  • Joined: 25-September 09

Re: DataGridView multiple rows

Posted 07 September 2012 - 04:47 PM

I did something similar to this a loooong time ago, but couldn't find it.
Here is how I think I went about doing it.
       For Each col As DataGridViewColumn In DataGridView1.Columns
            'Create teh columns that are on Form2 with this DGV's columns
            Form2.DataGridView1.Columns.Add(col.Name, col.HeaderText)
        Next
        'Loop through the collection of rows and columns
        For Each row As DataGridViewRow In DataGridView1.Rows
            'With each new row, add a row to the Form2 DGV.
            Form2.DataGridView1.Rows.Add()
            For Each col As DataGridViewColumn In DataGridView1.Columns
                'With each new column in the current row, copy the values using the 
                'indexes from the For...Each loops.
                Form2.DataGridView1.Rows(row.Index).Cells(col.Index).Value =
                    DataGridView1.Rows(row.Index).Cells(col.Index).Value
            Next
        Next
        Form2.Show()

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1