1,105,331 Community Members

Transfering items from listview to datagridview

Member Avatar
Zcast
Newbie Poster
1 post since Sep 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Hi All,

Trying to add items from listview to datagridview, but keep coming up with this error:
"Specified argument was out of the range of valid values.
Parameter name: rowIndex"

I can add one row with no errors, but when I try to add two or more at the same time, this error occurs.

This is my code:

For i As Integer = 0 To form.lvStops.SelectedItems.Count - 1

        For Each item As ListViewItem In form.lvStops.SelectedItems
            Dim row As New DataGridViewRow
            frmAddLoad.Instance.DataGridView1.Rows.Add(row)
            With row
                .Cells(0).Value = DateTime.Now
                .Cells(1).Value = form.lvStops.SelectedItems(i).Text
                .Cells(2).Value = form.lvStops.SelectedItems(i).SubItems(1).Text
                .Cells(3).Value = form.lvStops.SelectedItems(i).SubItems(2).Text
                .Cells(4).Value = form.lvStops.SelectedItems(i).SubItems(3).Text
                .Cells(5).Value = form.lvStops.SelectedItems(i).SubItems(4).Text
                .Cells(6).Value = form.lvStops.SelectedItems(i).SubItems(5).Text
                .Cells(7).Value = form.lvStops.SelectedItems(i).SubItems(6).Text
                .Cells(8).Value = form.lvStops.SelectedItems(i).SubItems(7).Text
                .Cells(9).Value = form.lvStops.SelectedItems(i).SubItems(8).Text
                .Cells(10).Value = form.lvStops.SelectedItems(i).SubItems(9).Text
                .Cells(11).Value = form.lvStops.SelectedItems(i).SubItems(10).Text
            End With


        Next
    Next

    Any help would be appreciated!

    Zcast
Member Avatar
john.knapp
Posting Whiz in Training
221 posts since Oct 2012
Reputation Points: 25 [?]
Q&As Helped to Solve: 22 [?]
Skill Endorsements: 4 [?]
 
0
 

Don't you need to add the row after setting the values?

Member Avatar
TnTinMN
Practically a Master Poster
640 posts since Jun 2012
Reputation Points: 418 [?]
Q&As Helped to Solve: 152 [?]
Skill Endorsements: 16 [?]
 
0
 

Try this pattern, it's one of many that will work.

  With DataGridView1.Rows(DataGridView1.Rows.Add())
     .Cells(0).Value = "hi"
  End With

"DataGridView1.Rows.Add()" returns the index of the row that is just added. Doing it this way you add an empty row that is templated to accept the values that the cells expect.

Edit: This assumes that you have defined the columns of the DGV elsewhere.

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article