user3107343
user3107343

Reputation: 2299

How to get count rows in AspxGridView?

I have a Gridview.I want to get count of Status=1 and Status=0 rows

    Id    Bla    Bla     Bla  Status

    1                         1 
    27                        0
    323                       1

<dx:ASPxGridView runat="server" ID="grid" Width="100%" >
            <Columns>

         some colums

         ...

         ...

</dx:GridViewDataTextColumn>
 <dx:GridViewDataTextColumn Caption="Status" FieldName="Online">
                </dx:GridViewDataTextColumn>    
        </Columns>
</dx:ASPxGridView>

How can I get counts ?

I want to display like this

  lblOnline.Text="Online Dealers : "+  countOnline.ToString();
  lblOffline.Text="Offline Dealers : "+ countOffline.ToString();

Upvotes: 0

Views: 4660

Answers (2)

Dhrumil
Dhrumil

Reputation: 3204

Try something like this.

foreach(GridViewRow row in GridView2.Rows)

{
  int status = Convert.toInt32(row.Cells['statuscolumnindex'].Text);
    if(status == 1)
      { countA++; }
    else
      { countB++; }
}

Here statuscolumnindex is the integer index of you Status column in the GridView. e.g. (row.Cells[2]) .Just the integer value. This is not the exact code, you will have to make it as per your requirement.

Hope this helps.

Upvotes: 1

alex.pulver
alex.pulver

Reputation: 2123

int countOnline = 0;
foreach (DataRow dr in dataTable.Rows)
{
    if (dr["Status"].Equals("1"))
    {
          countOnline++;
    }
}
int countOffline = dataTable.Rows.Count - countOnline;

You may also extract the Status column to an ArrayList and sort the list using ArrayList.Sort and count only the first sorted 0 values.

Upvotes: 1

Related Questions