How to loop through the whole gridview rows including the rows in other pages?

2.6k Views Asked by At

I know how to loop through the grid view rows :


foreach (GridViewRow oItem in GridView1.Rows)
{
    //
}

but what i want to do is looping through the whole gridview including the rows in the other pages if i enable the paging . how to do this ?

2

There are 2 best solutions below

1
On

You can use Cast<T> or OfType<T> to convert to IEnumerable<T>:

foreach (GridViewRow oItem in GridView1.Rows.OfType<GridViewRow>())
{
}

Or:

foreach (GridViewRow oItem in GridView1.Rows.Cast<GridViewRow>())
{
}

In this case it is correct to use both because Rows only contains element of GridViewRow. But you should not notice the different between two methods:

  1. Cast<T>: Casts the elements to the specified type.

  2. OfType<T>: Filters the elements based on a specified type.

2
On

@just_name, you need to remember that any manipulation with server-side objects like GridViewRow is worst way to work with data. If you need any data-driven manipulations - do it in source of data, not with view.