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

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

Marina

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

BoomBoom

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)
{
....
}
 
M

Marina

Right, that is the same thing. The Item property is the default property for
the DataView class.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top