We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,390 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Transfering items from listview to datagridview

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
3
Contributors
2
Replies
17 Hours
Discussion Span
5 Months Ago
Last Updated
3
Views
Zcast
Newbie Poster
1 post since Sep 2010
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

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

john.knapp
Posting Whiz in Training
221 posts since Oct 2012
Reputation Points: 25
Solved Threads: 22
Skill Endorsements: 3

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.

TnTinMN
Practically a Master Poster
640 posts since Jun 2012
Reputation Points: 418
Solved Threads: 148
Skill Endorsements: 13

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.0902 seconds using 2.65MB