How To Sort Datatable in c#.net

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"]);
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s