.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Sandeep Singh
Melody Anderson
Eminent IT

Home >> Code Snippets >> ADO.NET >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Filter DataTable

Posted By:Syed Shakeer Hussain       Posted Date: July 29, 2012    Points: 40    Category: ADO.NET    URL: http://www.dotnetspark.com  

There are many ways to filter DataTable.

1.) You can use the DataTable.Select method. However, the results are an array of DataRow which is not bindable.
2.) You can use a DataView which is efficient for sorting, filtering. All DataTables have a default DataView -- DataTable.DefaultView
3.) Or you can use LINQ.

DataView dv = new DataView(tableOne, "OrderID <> 'XXXX'", "OrderTime desc", DataViewRowState.CurrentRows);

Or transform the DataView back to a DataTable
DataTable newTable = new DataView(tableOne, "OrderID <> 'XXXX'",
                "OrderTime desc", DataViewRowState.CurrentRows).ToTable();
Or you can use LINQ
IEnumerable<DataRow> ordered = tableOne.AsEnumerable()
                .Where(i => i.Field<String>("OrderID ") != 'XXXX')               
                .OrderByDescending(i => i.Field<DateTime>("OrderTime "));

Or use LINQ to transform it to a DataTable
DataTable newTable = tableOne.AsEnumerable()
                 .Where(i => i.Field<String>("OrderID ") != 'XXXX')                 
                .OrderByDescending(i => i.Field<DateTime>("OrderTime "))


Further Readings:


No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend