Using ASP.Net C# DataTable DefaultView Sort Rows
In ASP.Net C#, DefaultView.Sort property of DataTable provides the functionality to sort the customized table view generated by the associated property of DataView class i.e. DefaultView property. It allows you to pass the sort expression similar to order by clause of SQL query. You just need to pass the column Name and sort direction to sort the rows of DataTable. When DefaultView.Sort property is used over the ASP.Net DataTable object, it customizes the view of DataTable within the same memory allocated to the instance of DataTable based on the sort expression. Consider the same C# sample code that we used in the previous tutorial: ASP.Net DataTable Select rows using DefaultView RowFilter to understand the use of Sort property.
C# Code for ASP.Net DataTable DefaultView Sort Rows
myDataTable.DefaultView.RowFilter = "unit_price < 40"; myDataTable.DefaultView.Sort = "unit_price desc"; GridView1.DataSource = myDataTable; GridView1.DataBind();
You can use both properties of DefaultView property of DataTable to sort the filtered rows. Or you can use one of them to Sort all the rows present in the DataTable. Above code will generate the following output:
Continue to next tutorial: ASP.Net C# Convert DataSet to ArrayList to learn how to convert dataset records into ArrayList using C# code.