Click here to Skip to main content
Sign Up to vote bad
good
See more: C#2.0
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)
and
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>
Posted 30 Jun '11 - 23:31
Edited 1 Jul '11 - 0:18


8 solutions

Why you are using ID ASC. You should use Preferance ASC
Datatable.DefaultView.Sort = "Preferance ASC"; 
  Permalink  
Comments
psychic6000 - 30 Nov '12 - 12:00
worked for me, thanks :)
This[^] is how I'd solve this problem.
  Permalink  
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!
  Permalink  
            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"]);
            }
  Permalink  
I think this is the easiest way to sort a Datatable..
 
dt.DefaultView.Sort = "Parameter_Name";
dt = dt.DefaultView.ToTable();
  Permalink  
Comments
B00SA - 28 Dec '12 - 20:37
Good suggestion. Works also with more than one column names.
All This Solution Are Not working for dates in the datatable
  Permalink  
  Permalink  
Comments
Nelek - 6 Nov '12 - 8:45
Did you notice that the question is from July 2011?
TableToConvert.DefaultView.Sort = "EVENT_ID";
DataView TableView = TableToConvert.DefaultView;
DataTable NewTable = TableView.ToTable();
 
The new Table will be sorted
  Permalink  
Comments
ProgramFOX - 28 Nov '12 - 11:59
Why do you post an answer to a question from 1 year ago? That doesn't make sense.

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

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 OriginalGriff 178
1 Tadit Dash 144
2 Santhosh G_ 140
3 Sergey Alexandrovich Kryukov 134
4 Espen Harlinn 120
0 Sergey Alexandrovich Kryukov 10,348
1 OriginalGriff 7,965
2 CPallini 4,241
3 Rohan Leuva 3,522
4 Maciej Los 3,184


Advertise | Privacy | Mobile
Web01 | 2.6.130523.1 | Last Updated 28 Nov 2012
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid