Click here to Skip to main content
12,406,795 members (61,005 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C#2.0 C#
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:31pm
Updated 1-Jul-11 0:18am
v3
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

Why you are using ID ASC. You should use Preferance ASC
Datatable.DefaultView.Sort = "Preferance ASC"; 
  Permalink  
Comments
psychic6000 30-Nov-12 12:00pm
   
worked for me, thanks :)
ali yeganeh 7-Apr-14 11:43am
   
this is good way
thanks
xyberice 1-Jul-16 11:11am
   
Doesn't work. You need to re-assign the DataTable (as shown in solution 6)
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 6

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:37pm
   
Good suggestion. Works also with more than one column names.
Member 10364869 28-Oct-13 7:50am
   
excellent....!!!!!!

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.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

This[^] is how I'd solve this problem.
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

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  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 4

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  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 8

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:59am
   
Why do you post an answer to a question from 1 year ago?
That doesn't make sense.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 7

  Permalink  
Comments
Nelek 6-Nov-12 8:45am
   
Did you notice that the question is from July 2011?
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 9

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
  Permalink  
v3
Comments
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
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 10

Just Use the following code for Reordering Rows in DataTable :
---------------------------------------------------------------
ds.Tables["dtPaymentScheduleTemplateInstallments"].DefaultView.Sort = "[Display Order] ASC";
ds.Tables["dtPaymentScheduleTemplateInstallments"].AcceptChanges();
dtgInstallments.DataSource = ds.Tables["dtPaymentScheduleTemplateInstallments"].DefaultView;

Where :
--------
ds --> DataSet
dtPaymentScheduleTemplateInstallments --> DataTable within DataSet
[Display Order] --> ColumnName on behalf we are Reordering
  Permalink  
v2
Comments
CHill60 2-Aug-13 13:03pm
   
Did you realise that this question is from 2 years ago and already resolved?

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160730.1 | Last Updated 2 Aug 2013
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100