0

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.

Edited by CodingSource

2
Contributors
1
Reply
12
Views
2 Years
Discussion Span
Last Post by djjeavons
0

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

Edited by djjeavons

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.