We have two columns in a DataTable
, like so:
COL1 COL2
Abc 5
Def 8
Ghi 3
We're trying to sort this datatable
based on COL2
in decreasing order.
COL1 COL2
ghi 8
abc 4
def 3
jkl 1
We tried this:
ft.DefaultView.Sort = "occr desc";
ft = ft.DefaultView.ToTable(true);
but, without using a DataView
, we want to sort the DataTable
itself, not the DataView
.
It turns out there is a special case where this can be achieved. The trick is when building the DataTable, collect all the rows in a list, sort them, then add them. This case just came up here.
TL;DR
use
tableObject.Select(queryExpression, sortOrderExpression)
to select data in sorted mannerComplete example
Complete working example - can be tested in a console application:
Output
There is 2 way for sort data
1) sorting just data and fill into grid:
2) sort default view that is like of sort with grid column header:
Did you try using the
Select(filterExpression, sortOrder)
method on DataTable? See here for an example. Note this method will not sort the data table in place, if that is what you are looking for, but it will return a sorted array of rows without using a data view.This will help you...
Its Simple Use .Select function.
And it's done......Happy Coding