FirstCape
FirstCape

Reputation: 639

For Each GridviewRow Loop, with where clause?

Is there any easier way of using the code:

For Each gvRow As GridViewRow In gvGridviewExample.Rows
    If gvRow.RowType = DataControlRowType.DataRow Then
        'Do Something
    End If
Next

Such as when using a iEnumerable you theoritically (psuedo code style) add a:

gvGridviewExample.Where(Function(chR) chR.Row = DataRow)

I just wondered if there is an easier way of coding this out?

Thanks,

Firstcape

Upvotes: 1

Views: 3438

Answers (2)

Carsten
Carsten

Reputation: 11606

In addition to Tim's answer, which is 100% correct for your specific case, you can use in similar situations, i.e. when you only want to iterate over all visible rows:

gvGridViewExample.Rows
    .Where(Function(r) r.Displayed = True)  ' Apply filter
    .ToList()
    .ForEach(Function(x) x.DoSomething())  ' Do something...

or

For Each row in gvGridViewExample.Rows.Where(Function(r) r.Displayed = True)
    row.DoSomething()
Next

Upvotes: 0

Tim Schmelter
Tim Schmelter

Reputation: 460158

Apart from the fact that your first snippet actually is very simple and clear, the Rows property of a GridView returns only DataRows. So you don't need to check the type at all.

MSDN

The Rows property (collection) is used to store the data rows in a GridView control.

Only rows with their RowType property set to DataControlRowType.DataRow are stored in the Rows collection. The GridViewRow objects that represent the header, footer, and pager rows are not included in the collection.

You can only have different types in events like RowCreated or RowDataBound. If you want to access the header, footer or pager outside of these events you can use the appropriate properties of the GridView like HeaderRow or FooterRow.

Upvotes: 3

Related Questions