Click here to Skip to main content
15,512,747 members
Please Sign up or sign in to vote.
3.38/5 (5 votes)
See more:
Hello, I have Datatable like

   Date        Preferance
30/07/2011      0
02/03/2011      1
10/12/2011      2
01/12/2011      3

I am trying to sort prefenaces column as per date(Samllest date will get first prefenace),i am trying to get values in datatable like

 Date       Preferance
30/07/2011      1
02/03/2011      0
10/12/2011      3
01/12/2011      2

i am trying to sorts dates in datatable,
i have tried
Datatable.Select(filterExp, sortExp, DataViewRowState.CurrentRows)
Datatable.DefaultView.Sort = "ID ASC"; 

but both this are not working this method takes date as string

What to do for this,please help </pre>
Updated 16-Jun-22 3:39am

Why you are using ID ASC. You should use Preferance ASC
Datatable.DefaultView.Sort = "Preferance ASC"; 
Share this answer
psychic6000 30-Nov-12 12:00pm    
worked for me, thanks :)
ali yeganeh 7-Apr-14 11:43am    
this is good way
xyberice 1-Jul-16 11:11am    
Doesn't work. You need to re-assign the DataTable (as shown in solution 6)
I think this is the easiest way to sort a Datatable..

dt.DefaultView.Sort = "Parameter_Name";
dt = dt.DefaultView.ToTable();
Share this answer
B00SA 28-Dec-12 20:37pm    
Good suggestion. Works also with more than one column names.
Member 10364869 28-Oct-13 7:50am    

This is the true answer....!!
spencepk 22-Nov-13 8:21am    
Roger that... works a treat!
itman2 30-Dec-14 11:35am    
tanks alot for this solution mr shakthi500.
necsa59 6-Nov-16 21:21pm    
Thanks, excellent
This[^] is how I'd solve this problem.
Share this answer
Google, my friend, Google.
A very quick search using your subject as the search term lead me to MSDN: Filtering and Sorting Directly in Data Tables[^]

Next time, try google first!
Share this answer
DataTable _dt = new DataTable();
_dt.Columns.Add("Date", typeof(DateTime));
_dt.Columns.Add("Preferance", typeof(int));

_dt.Rows.Add("30/07/2011", 0);
_dt.Rows.Add("02/03/2011", 1);
_dt.Rows.Add("10/12/2011", 2);
_dt.Rows.Add("01/12/2011", 3);

DataView _dv = new DataView(_dt);
_dv.Sort = "Date ASC";

for (int i = 0; i < _dv.Count; i++)
    Console.WriteLine(Convert.ToDateTime(_dv[i]["Date"]).ToString("dd/MM/yyyy") + "\t" + _dv[i]["Preferance"]);
Share this answer
TableToConvert.DefaultView.Sort = "EVENT_ID";
DataView TableView = TableToConvert.DefaultView;
DataTable NewTable = TableView.ToTable();

The new Table will be sorted
Share this answer
Thomas Daniels 28-Nov-12 11:59am    
Why do you post an answer to a question from 1 year ago?
That doesn't make sense.
DataTable dtCategory = dsHotelResponse.Tables["Category"].DefaultView.ToTable(true,"ApartmentNo", "Gross").Select("ApartmentNo='" + (roomNo) + "'", "Gross asc").CopyToDataTable();

roomno is use for to filter dynamic values for ApartmentNo change it depend upon your condiction
Share this answer
CHill60 20-Jul-13 15:33pm    
Why are you posting a solution to a question from 2 years ago - and where's the sort?
chirag.jdk 22-Jul-13 7:50am    
chill60 check my code correctly you got your solution here Gross asc is filter express and i give this solution bcoz now one provide solution for sort and condition together
Share this answer
Nelek 6-Nov-12 8:45am    
Did you notice that the question is from July 2011?
Just Use the following code for Reordering Rows in DataTable :
ds.Tables["dtPaymentScheduleTemplateInstallments"].DefaultView.Sort = "[Display Order] ASC";
dtgInstallments.DataSource = ds.Tables["dtPaymentScheduleTemplateInstallments"].DefaultView;

Where :
ds --> DataSet
dtPaymentScheduleTemplateInstallments --> DataTable within DataSet
[Display Order] --> ColumnName on behalf we are Reordering
Share this answer
CHill60 2-Aug-13 13:03pm    
Did you realise that this question is from 2 years ago and already resolved?
//Sorting the Table
            DataView dv = dt.DefaultView;
            dv.Sort = "ParamValue asc";
            DataTable sortedtable1 = dv.ToTable();
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900