Datagrid Looping question

asked 2022-12-22T20:20:39.57+00:00
Karson Mac 41 Reputation points

I've been working at this for hours and I'm unable to solve. Anytime I load the DataGrid the query starts at the top and returns duplicate values. For example, if I have 90 items in the grid it will find the entry that meets the requirement then loop back and find the first one again before looking for the second one. Probably not explaining my issue all that well.

Here's a snippet in the code. Any help would be appreciated..

Thanks,

For i As Integer = 0 To DataGridView1.Rows.Count - 1
If Me.DataGridView1.Rows(i).Cells(6).Value > strmileagemax Then
Me.DataGridView1.Rows(i).Cells(6).Style.BackColor = Color.Red
Me.DataGridView1.Rows(i).Cells(6).Style.ForeColor = Color.White
Me.DataGridView1.Rows(i).Cells(6).ToolTipText = "OIL CHANGE DUE"
strcarNumber1 = Me.DataGridView1.Rows(i).Cells(1).Value
stremailsubject = strcarNumber1
MsgBox("Mileage " & stremailsubject)
End If

    Next  
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
1,825 questions
No comments
{count} votes

Accepted answer
  1. answered 2022-12-22T21:06:24.037+00:00
    LesHay 5,661 Reputation points

    Hi
    Testing your code here.
    I have a standard DataGridView on Form1 with 7 columns and a standard Button. Added some random data (see code). Put your code into the Button.Click event Handler and changed a couple of lines in your code as they were redundant for this test.
    Here is all the code I used for testing. I got the correct cell style changes and a MessageBox pop-up with a list of overdue.
    NOTE: to satisfy the Option Strict, I had to add a Cint() to one occurrance and convert to string in another. (cell contents are type Object)

    Option Strict On  
    Option Explicit On  
    Public Class Form1  
      Dim strmileagemax As Integer = 10000  
      Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click  
        DataGridView1.Rows.Clear()  
        Dim r As New Random  
        Dim lst As String = "Overdue items: "  
        For i As Integer = 1 To 10  
          With DataGridView1  
            .Rows.Add(Nothing, i, Nothing, Nothing, Nothing, Nothing, r.Next(9995, 10010))  
          End With  
        Next  
        For i As Integer = 0 To DataGridView1.Rows.Count - 1  
          If CInt(Me.DataGridView1.Rows(i).Cells(6).Value) > strmileagemax Then  
            Me.DataGridView1.Rows(i).Cells(6).Style.BackColor = Color.Red  
            Me.DataGridView1.Rows(i).Cells(6).Style.ForeColor = Color.White  
            Me.DataGridView1.Rows(i).Cells(6).ToolTipText = "OIL CHANGE DUE"  
            'strcarNumber1 = Me.DataGridView1.Rows(i).Cells(1).Value  
            'stremailsubject = strcarNumber1  
            '    MsgBox("Mileage " & stremailsubject)  
            lst &= Me.DataGridView1.Rows(i).Cells(1).Value.ToString & "  "  
          End If  
        Next  
        MessageBox.Show(lst)  
      End Sub  
    End Class  
    
    No comments

1 additional answer

Sort by: Most helpful
  1. answered 2022-12-22T22:33:32.107+00:00
    Karson Mac 41 Reputation points

    Thanks really appreciate the help

    No comments