I have the following method:
private DataTable getsortedtable(DataTable dt)
{
dt.DefaultView.Sort = "Name desc";
//I would need to return the datatable sorted.
}
My issue is that I cannot change the return type of this method and I have to return a DataTable but i would like return it sorted.
Are there any magic hidden property of dt.DefaultView
to return the dt sorted?
Property ValueA string that contains the column name followed by "ASC" (ascending) or "DESC" (descending). Columns are sorted ascending by default. Multiple columns can be separated by commas.
The DataView provides several ways of sorting and filtering data in a DataTable: You can use the Sort property to specify single or multiple column sort orders and include ASC (ascending) and DESC (descending) parameters.
A DataView enables you to create different views of the data stored in a DataTable, a capability that is often used in data-binding applications. Using a DataView, you can expose the data in a table with different sort orders, and you can filter the data by row state or based on a filter expression.
private DataTable getSortedTable(DataTable dt)
{
dt.DefaultView.Sort = "columnName DESC";
return dt.DefaultView.ToTable();
}
do this
private DataTable getsortedtable(DataTable dt)
{
//do the operation for sort
return dataView.ToTable();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With