1

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 ?

cuongle
  • 74,024
  • 28
  • 151
  • 206
Anyname Donotcare
  • 11,113
  • 66
  • 219
  • 392

2 Answers2

2

@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.

DrAlligieri
  • 211
  • 5
  • 10
1

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.

cuongle
  • 74,024
  • 28
  • 151
  • 206
  • hmm,thanks ,but this fixes my problem . If the grid for example has 5 rows and i'm in the last page which has a one row . the loop count is `1` not `5` !!! I wanna to loop through the whole pages not the selected page only . – Anyname Donotcare Oct 07 '12 at 09:10