Geeks With Blogs

Michael Freidgeim's Blog MS .Net Development

I have a filtered DataView and I wanted to search it using different keys that was used for sorting.

Ther is no function out-of-the-box, so I created a helper method

public static DataView CloneDataView(DataView vwSource,string newSort)      
 {
    return new DataView(vwSource.Table ,vwSource.RowFilter,newSort,vwSource.RowState) ;
}

 Another function to create new DataView with extra filter:

        public static DataView NewDataViewWithExtraFilter(DataView vwSource, string newFilter)
        {
            string sFilter = vwSource.RowFilter;
            if (String.IsNullOrEmpty(sFilter))
            {
                sFilter = newFilter;
            }
            else
            {
                sFilter += String.Format(" and ( {0} )", newFilter);
            }
            DataView newDV = new DataView(vwSource.Table, sFilter, vwSource.Sort, vwSource.RowStateFilter);
           return newDV;
        }

Posted on Thursday, March 2, 2006 1:20 PM .Net Framework | Back to top


Comments on this post: Function to Clone DataView with new sort column(s)

# re: Function to Clone DataView with new sort column(s)
Requesting Gravatar...
It helped me. Thanks for the code.
Left by Thankful on Aug 31, 2008 3:12 AM

# re: Function to Clone DataView with new sort column(s)
Requesting Gravatar...
Nice post,

Can you put some exampleas for rowSelected as well. I have some trouble working with that
Left by web development company on Aug 11, 2009 1:19 PM

# re: Function to Clone DataView with new sort column(s)
Requesting Gravatar...
nice one. many thanks. helped me a lot.
Left by sandy on Jun 01, 2011 6:22 AM

# re: Function to Clone DataView with new sort column(s)
Requesting Gravatar...
Regarding adding an additional filter, there is one minor issue you should take into account:

The previous filter could be a non-parethesized logical combination, e.g.
Filter = "Name='Fred' or Cost=3"

Assume the new you want to add is "Index < 5". We expect this to filter out ALL records with an index of 5 or higher. We add on the new filter by YOUR method. . It becomes:
Filter = "Name = 'Fred' or Cost=3 and (Index < 5)

To our surprise, we get several records with an index of 5 or higher!! This is a problem! How did this happen?

Well by the order of operations, AND is taken first, and OR is next. So in effect, the resulting filter is actually equivalent to this:
Filter = "Name = 'Fred' or (Cost=3 and Index < 5)

Note that now, even if a record has an Index >= 5, it will still pass the filter as long as Name=Fred, regardless of Cost or Index.

To fix this problem, you should append the new filter as follows:

sFilter = String.Format("( {0} ) and ( {1} )", sFilter, newFilter);

Then the resulting filter in the above discussion would be:
"(Name = 'Fred' or Cost=3) and (Index < 5)"
Which would indeed filter out all records with Index >= 5 as well as all records that fail the existing filter. This is the expected result.
Left by ricovox on Sep 15, 2012 1:42 AM

# re: Function to Clone DataView with new sort column(s)
Requesting Gravatar...
thank you.
Left by james on Dec 28, 2012 8:30 PM

Your comment:
 (will show your gravatar)


Copyright © Michael Freidgeim | Powered by: GeeksWithBlogs.net | Join free