In my form I have a button and a label (and of course, a datagridview) in my form. What I want to accomplish is when the user clicks the button, it'll show how many certain text are in the datagridview column.

I have column named "Domestic_Animals" and there are two separate panels, one for dog and one for cat. When the user clicks the "Count How Many Dogs in the GridView" button, it'll show how many "dogs" text are in the gridview. Same with the "Cats" button.

/*DOMESTIC_ANIMALS/
Dog
Cat
Cat
Cat
Dog

<btnDogs> (the user clicks this) : Label1.Text = 2
<btnCats< (the user clicks this): Label2.Text = 3

For further information, my datagridview is data bounded. So the user may add more dogs and cats in an INSERT query. So the answer should not be always '2' for dogs or '3' for cats but instead that the code really counts how many cats word or dogs word are in the datagridview.

Any suggestion will help me.

Dani AI

Generated

Brief summary: count from the underlying data, not the grid UI. is right to query the bound data source rather than scanning visible cells; below are a few practical, safe alternatives depending on how your DataGridView is bound and a couple of gotchas to watch.

If the grid is bound to a DataTable:

Dim dt = TryCast(DataGridView1.DataSource, DataTable)
If dt IsNot Nothing Then
    Dim dogs() As DataRow = dt.Select("Domestic_Animals = 'Dog'")
    Label1.Text = dogs.Length.ToString()
End If

If the grid is bound through a BindingSource (nice when you want the UI-filter behavior):

Dim bs = TryCast(DataGridView1.DataSource, BindingSource)
If bs IsNot Nothing Then
    bs.Filter = "Domestic_Animals = 'Dog'"
    Label1.Text = bs.Count.ToString()
    bs.RemoveFilter()
End If

If the source is a list/collection of objects use LINQ over that list (or a simple loop) and do a case-insensitive compare:

Dim list = TryCast(DataGridView1.DataSource, List(Of Animal))
If list IsNot Nothing Then
    Dim count = list.Count(Function(a) String.Equals(a.Type, "Dog", StringComparison.OrdinalIgnoreCase))
    Label1.Text = count.ToString()
End If

Troubleshooting and tips:

  • Trim and normalize values (Trim, ToUpper/ToLower or StringComparison) to avoid mismatches from extra spaces or case differences.
  • Escape single quotes if user input goes into a Select/Filter string.
  • Handle DBNull when reading DataRow values.
  • For very large tables, run a COUNT(...) on the database side (SQL) instead of pulling all rows into memory.
  • If counting happens off the UI thread, update Label via Invoke to avoid cross-thread exceptions.

Choose the approach that matches your binding type; wrap the logic in a small helper (CountAnimals("Dog")) and call it from the button click so counts stay correct after inserts/refreshes.

Hi

You state that your DataGridView is data bounded. If it is bound to a DataTable then you could use the following lambda statement to count the occurrences of a given value. For example:

        Dim dt As DataTable = CType(DataGridView1.DataSource, DataTable)
        Dim count As Integer = dt.AsEnumerable().Count(Function(dr) dr.Field(Of String)("Animal").Equals("Cat"))

HTH

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.