How do I use "foreach" with a DataView RowFilter?

  • Thread starter Thread starter BoomBoom
  • Start date Start date
B

BoomBoom

I have a DataView (dvH) attached to a DataSet table to which I applied a
RowFilter. The filter yields three records from a total of 404. I am trying
to process these records using a foreach statement, but it insists on
processing the entire 404 recordset rather than the three.

This is the statement I tried using:

foreach (DataRow r in dvH.Table.Rows)
{
....
}

I also tried:

foreach (DataRow r in dvH)
{
....
}

The latter compiled but fails at runtime.
 
That is because you are accessing the Rows property of the Table property.
The Table property points to the datatable the view is on - which of course
has just the original data, so you are seeing everything, as if you were
looking through the original datatable itself.

Try using the Item property, to get the filtered rows.
 
Marina said:
That is because you are accessing the Rows property of the Table property.
The Table property points to the datatable the view is on - which of course
has just the original data, so you are seeing everything, as if you were
looking through the original datatable itself.

Try using the Item property, to get the filtered rows.

Actually, I stumbled on the following solution that seems to work:

foreach (DataRowView h in dvH)
{
....
}
 
Right, that is the same thing. The Item property is the default property for
the DataView class.
 
Back
Top