Home All Groups Group Topic Archive Search About

Iterate a datatable filtering on the default view.

Author
8 Feb 2007 5:58 PM
Chris
I have a data table for which I have set the default view's rowfilter.
If I bind to a control everything is fine. If I go through the table with a
foreach I see everything, not my filtered data.
How would I iterate through with respect to the rowfilter?


     MyDataset.Tables["row"].DefaultView.RowFilter = "region='US'";

        foreach (DataRow dr in MyDataset.Tables["row"].Rows)
        {
            this.lblFilter.Text = lblFilter.Text + " --- " +
                dr["region"].ToString();
        }

TIA,

Chris

Author
9 Feb 2007 8:43 AM
Miha Markic [MVP C#]
You should iterate all DataRowViews, like
for (int i=0; i<MyDataset.Tables["row"].DefaultView.Count; i++)
{
    DataRowView rowView = MyDataset.Tables["row"].DefaultView[i];
}

And then, if you need to, access the underlying row through rowView.Row
property.

--
Miha Markic [MVP C#, INETA Country Leader for Slovenia]
RightHand .NET consulting & development www.rthand.com
Blog: http://cs.rthand.com/blogs/blog_with_righthand/

Show quote
"Chris" <chris01@newsgroups.nospam> wrote in message
news:%23H9PBr6SHHA.3440@TK2MSFTNGP03.phx.gbl...
>I have a data table for which I have set the default view's rowfilter.
> If I bind to a control everything is fine. If I go through the table with
> a
> foreach I see everything, not my filtered data.
> How would I iterate through with respect to the rowfilter?
>
>
>     MyDataset.Tables["row"].DefaultView.RowFilter = "region='US'";
>
>        foreach (DataRow dr in MyDataset.Tables["row"].Rows)
>        {
>            this.lblFilter.Text = lblFilter.Text + " --- " +
>                dr["region"].ToString();
>        }
>
> TIA,
>
> Chris
>
Author
9 Feb 2007 2:05 PM
WenYuan Wang
Hi Chris,

I totally agree with Miha. That method should work.

Additional, we can also use "foreach" statement with it.
foreach (DataRowView drv in in MyDataset.Tables["row"].DefaultView)
        {    ... }

Have a great day.
Sincerely.
Wen Yuan
Author
11 Feb 2007 2:11 PM
Cor Ligthert [MVP]
Chris,

I agree with Miha and WenYuang, but be aware that you have given your table
the name "row" a little bit strange name for a table. Have a look on it, if
that was what you was meaning?

Cor

Show quote
"Chris" <chris01@newsgroups.nospam> schreef in bericht
news:%23H9PBr6SHHA.3440@TK2MSFTNGP03.phx.gbl...
>I have a data table for which I have set the default view's rowfilter.
> If I bind to a control everything is fine. If I go through the table with
> a
> foreach I see everything, not my filtered data.
> How would I iterate through with respect to the rowfilter?
>
>
>     MyDataset.Tables["row"].DefaultView.RowFilter = "region='US'";
>
>        foreach (DataRow dr in MyDataset.Tables["row"].Rows)
>        {
>            this.lblFilter.Text = lblFilter.Text + " --- " +
>                dr["region"].ToString();
>        }
>
> TIA,
>
> Chris
>
Author
13 Feb 2007 6:37 AM
WenYuan Wang
Hi Chris,

We haven't heard from two days. I just want to check if you have any
further question.
Please fell free to reply me if there is anything we can help with.

Have a great day!
Wen Yuan

AddThis Social Bookmark Button