Function to Clone DataView with new sort column(s)

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 @ Thursday, March 2, 2006 1:20 PM
Print

Comments on this entry:

# re: Function to Clone DataView with new sort column(s)

Left by Thankful at 8/31/2008 3:12 AM
Gravatar
It helped me. Thanks for the code.

# re: Function to Clone DataView with new sort column(s)

Left by web development company at 8/11/2009 1:19 PM
Gravatar
Nice post,

Can you put some exampleas for rowSelected as well. I have some trouble working with that

# re: Function to Clone DataView with new sort column(s)

Left by sandy at 6/1/2011 6:22 AM
Gravatar
nice one. many thanks. helped me a lot.

# re: Function to Clone DataView with new sort column(s)

Left by ricovox at 9/15/2012 1:42 AM
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.

# re: Function to Clone DataView with new sort column(s)

Left by james at 12/28/2012 8:30 PM
Gravatar
thank you.

Your comment:



(not displayed)

 
 
 
 

Live Comment Preview:

 
«December»
SunMonTueWedThuFriSat
30123456
78910111213
14151617181920
21222324252627
28293031123
45678910