[VB] += counting doesn't work for each row

M B 41 Reputation points
2021-05-08T14:55:42.423+00:00

Hello, I need to count how many rows got a specific text in Column1.
It looks like it works but the number is still 1, but there are more rows with this text. It looks like it doesn't count +1 for every row. How to do that?

Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click

    Dim count As Int32 = 0

    For Each row As DataGridViewRow In DataGridView1.Rows
        If row.Cells("Column1").Value = ("CountMe") Then
            count += 1
        End If
    Exit For
    Next
    CountLabel.Text = count
    If (count > 0) Then
        MessageBox.Show("Exist!")
    Else
        MessageBox.Show("Nothing here!")
    End If
End Sub

Thanks.

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,835 questions
0 comments No comments
{count} votes

Accepted answer
  1. WayneAKing 4,921 Reputation points
    2021-05-08T21:28:14.023+00:00

    For Each row As DataGridViewRow In DataGridView1.Rows
    If row.Cells("Column1").Value = ("CountMe") Then
    count += 1
    End If
    Exit For
    Next

    Why do you have

    Exit For

    in there? That causes the loop to end after one
    iteration. Remove it.

    • Wayne

2 additional answers

Sort by: Oldest
  1. Ken Tucker 5,846 Reputation points
    2021-05-08T18:40:04.677+00:00

    Maybe there is a space after CountMe, I would try this instead

         If row.Cells("Column1").Value.ToString().Trim() = ("CountMe") Then
             count += 1
         End If
    

  2. Karen Payne MVP 35,036 Reputation points
    2021-05-09T13:14:24.457+00:00

    You can utilize a lambda statement to combine logic to get the count

    Public Class Form1
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            DataGridView1.Rows.Add(New Object() {"CountMe"})
            DataGridView1.Rows.Add(New Object() {"CountMe"})
            DataGridView1.Rows.Add(New Object() {"Other"})
            DataGridView1.Rows.Add(New Object() {"CountMe"})
            DataGridView1.Rows.Add(New Object() {"CountMe"})
            DataGridView1.Rows.Add(New Object() {"CountMe"})
            DataGridView1.Rows.Add(New Object() {"Other"})
        End Sub
    
        Private Sub CountButton_Click(sender As Object, e As EventArgs) Handles CountButton.Click
    
            Dim count = DataGridView1.Rows.OfType(Of DataGridViewRow).
                    Count(Function(row) Not row.IsNewRow AndAlso CStr(row.Cells("Column1").Value) = "CountMe")
    
            CountLabel.Text = count.ToString()
    
            If (count > 0) Then
                MessageBox.Show("Exist!")
            Else
                MessageBox.Show("Nothing here!")
            End If
    
        End Sub
    End Class
    

    Case insensitive

    Private Sub CountButton_Click(sender As Object, e As EventArgs) Handles CountButton.Click
        Dim valueToFind = "CountMe"
        Dim count = DataGridView1.Rows.OfType(Of DataGridViewRow).
                Count(Function(row) Not row.IsNewRow AndAlso
                                    String.Compare(CStr(row.Cells("Column1").Value), valueToFind,
                                                   StringComparison.InvariantCultureIgnoreCase) = 0)
    
        CountLabel.Text = count.ToString()
    
        If (count > 0) Then
            MessageBox.Show("Exist!")
        Else
            MessageBox.Show("Nothing here!")
        End If
    
    End Sub
    
    0 comments No comments