Home > Blockchain >  How can I get the total number of certain text in my `GridView` and display in a label?
How can I get the total number of certain text in my `GridView` and display in a label?

Time:10-01

I want to display the total number of certain text in a column in a label in my web app So in my GridView I have a column that contains two text. one is E1 and the other is E3. Now what I would like to do is have two labels displaying the total count of E1 and the second label displaying the total count of E3.

So if this is my Column:

| Type  |
| E1    |
| E3    |
| E3    |
| E1    |
| E1    |

Something similar to this

Total E1 = 3
Total E3 = 2

I have no idea on how to achieve this so hoping someone can point me in the right direction?

Thanks

CodePudding user response:

You could try something like this. Just count the Cells whos value is your desired data, e.g. a string:

        int iCount = 0;
        for (int i = 0; i < GridView.RowCount-1; i  )
        {
            for (int j = 1; j < GridView.ColumnCount - 1; j  )
            {
                if (GridView[j, i].Value.ToString() == "E1")
                    iCount  ;
            }
        }

CodePudding user response:

You can try this:

int CountItem1 = 0;
int CountItem2 = 0;
int ColumnNumber = 2; //This is the column you want to verify. Starts from 0 to GridViewOffice365.ColumnNumbers -1 
for each (GridViewRow row in GridViewOffice365.Rows)
{
    if (row.Cells[ColumnNumber].Text == "E1")
    {
       CountItem1  ;
    }
    else if (row.Cells[ColumnNumber].Text == "E3")
    {
       CountItem2  ;
    }
}
lblE1.Text = "Total E1: " CountItem1.ToString();
lblE3.Text = "Total E3: "   CountItem2.ToString();
  • Related