Click here to Skip to main content
15,910,981 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How can we sort datatables in C#.net?
Posted
Comments
Rajesh Anuhya 9-Nov-11 7:14am    
Are you searched in Google...????

you can sort datatables by using Sort property

C#
datatables1.DefaultView.Sort = "column1"; 
 
Share this answer
 
You can't sort DataTable directly,
for to sort DataTable convert it to GrideView

or convert it into DataView
here is the sample code that may helps you.
C#
DataTable dt = new DataTable();
           DataRow dr;

           // Define the columns of the table.
           dt.Columns.Add(new DataColumn("IntegerValue", typeof(Int32)));
           dt.Columns.Add(new DataColumn("StringValue", typeof(string)));
           dt.Columns.Add(new DataColumn("CurrencyValue", typeof(double)));

           // Populate the table with sample values.
           for (int i = 0; i < 5; i++)
           {
               dr = dt.NewRow();

               dr[0] = i;
               dr[1] = "Item " + i.ToString();
               dr[2] = 5.23 * ((5-i) + 1);

               dt.Rows.Add(dr);
           }
           DataView dv = new DataView(dt);
           dv.Sort = "CurrencyValue";
           dt = dv.ToTable();

sort property of DataView takes string type, that contains the column name followed by "ASC" (ascending) or "DESC" (descending). Columns are sorted ascending by default. Multiple columns can be separated by commas.
C#
dv.Sort = "CurrencyValue DESC";
 
Share this answer
 
v2
Comments
Prashant Srivastava LKO 9-Nov-11 7:16am    
Could we Convert datatable into HTML table
 
Share this answer
 
v2
Google! It found this as the top answer, using your subject as the query: Filtering and Sorting Directly in Data Tables[^]
 
Share this answer
 
You can filter and sort the contents of a data table directly by calling a table's Select method.
Try
MSDN-[Filtering and Sorting Directly in Data Tables][^]
 
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