Sunday, October 11, 2009

DataView.Table - Problem to Persist Sort or Filter View

I was trying to Sort data with DataView and was trying to convert Dataview to table. I have notice even though view is sorted, when you try to convert it to table it would return only default sort.


Example:
//I was trying to perform something as following
DataTable dtGrid = GetData();
DataView dvSort = new DataView(dtGrid);
dvSort.Sort = "CreationDate DESC";
dtGrid = dvSort.Table; //Will Not Persist Sort Order

In above example even though view is in sorted order, when i tried to convert it to table it would return only default view. In order to get Sorted view (Persist Sort order) instead of using DataView.Table you should use DataView.ToTable() method

So if you changed above code with following it would start working as expected.

DataTable dtGrid = GetData();
DataView dvSort = new DataView(dtGrid);
dvSort.Sort = "CreationDate DESC";
dtGrid = dvSort.ToTable(); //Persist Sort Order

4 comments:

Anonymous said...

Table and ToTable are two different methods. .Table does not convert your dataview to a table, but gives you back the table the dataview is based on. Certainly, .Table does not preserve the filter/sort you have commited on the dataview.

On the other hand, .ToTable will convert your filtered / sorted dataview to a new table.

Anonymous said...

"Anonymous" - you are a genius. That's what he said and his whole point.

Cotton Yarn said...

thanks for this useful and informative posting with us. i like it very much. thanks for sharing this.

Volcom Stickers said...

Thanks for the info i am mostly caught in this problem you solve my problem.

Most Recent Post

Subscribe Blog via Email

Enter your email address:



Disclaimers:We have tried hard to provide accurate information, as a user, you agree that you bear sole responsibility for your own decisions to use any programs, documents, source code, tips, articles or any other information provided on this Blog.
Page copy protected against web site content infringement by Copyscape