Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Datagridview row.index 1

Status
Not open for further replies.

realm174

Programmer
Jan 3, 2002
154
CA
Hi guys, I searched and googled, but I still can't wrap my head around this one. First, a small section of code pertaining to my issue:

Code:
Restart:
        For Each row In DataGridView1.Rows
            If row.Index = -1 Then Exit For
            If (DataGridView1.Item(1, row.Index).Value = True) Or _
              (DataGridView1.Item(1, row.Index).Value = CheckState.Checked) Then
                count = count + 1
                If count > Val(Trim(ret)) Then
                    Exit Sub
                End If
                Dim SearchItem As String = DataGridView1.Item(0, row.Index).Value
                lblprocessing.Text = "Processing " & SearchItem
                Me.ProgressBar.Value = count

                Me.Refresh()
                MainForm.SearchBox.Text = SearchItem
                MainForm.AmazonSearch(SearchItem, "All", BatchMode)
                If RecordAdded Then
                    DataGridView1.Rows.Remove(row)
                    MainForm.Close()
                    RecordAdded = False                    
                    'GoTo restart
                End If
            End If
        Next

The intention here is to search for entries listed in a datagridview. If the item is found, it is added to an Access table, and removed from the datagrid, otherwise, it remains in the datagrid.

(RecordAdded is a public var being setup by one of the called sub, to determine if the record was added to the table or not.)

My problem happens when I remove the row from the datagrid. Everything "shifts up", so the for each ends up skipping entries.

My ugly solution was to have a "goto restart" as you can see commented out, but that's definitely not efficient, as it then restarts from the beginning of the datagraidview list and searches for some that have already been searched.

I can't change row.index, as it is read only.

Any ideas any one??



Cheers,

Realm174
 
Alright... went a different way about it, and it's working perfectly:

Code:
Dim X As Integer = 0
While X < DataGridView1.Rows.Count
(...)
   if recordadded then
     datagridview1.rows.removeat(x)
     x=x-1
   end if
   x=x+1
End While


Cheers,

Realm174
 
Count through the datagridview backwards, like so:

Code:
[red]Note: for this to work, DataGridView.AllowUserToAddRows must be set to False, OR the For...Next count should start at DataGridView1.Rows.Count - 2[/red]


Dim row As DataGridViewRow

DataGridView1.AllowUserToAddRows = False

For r As Integer = DataGridView1.Rows.Count - 1 To 0 Step -1

    row = DataGridView1.Rows(r)

    If r Mod 2 = 0 Then
        DataGridView1.Rows.Remove(row)
    End If

Next

MsgBox(DataGridView1.Rows.Count)

This way when you remove the processed rows, the only rows that are shifted are ones already looped through.




I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top