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.

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.