question

MB-2323 avatar image
0 Votes"
MB-2323 asked MB-2323 commented

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

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
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

WayneAKing-0228 avatar image
0 Votes"
WayneAKing-0228 answered MB-2323 commented

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

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Haha! I knew it will be something easy to fix. Thank you :-)
Working.

0 Votes 0 ·
vb2ae avatar image
0 Votes"
vb2ae answered MB-2323 edited

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
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Still 1 after changing to your code.

0 Votes 0 ·
karenpayneoregon avatar image
0 Votes"
karenpayneoregon answered karenpayneoregon edited

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


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.