Click here to Skip to main content
15,868,141 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
HI All,
I want to sort the columns of the DATA VIEW SORT in Ascending or Descending.
My Existing code works fine for string sorting but when it comes to numeric sorting it does wrongly.
For Example,
When I sort a column in ascending order which has numbers, it sorts like,
1
12
123

2
23
234

3
34
345

But I want in this order
1
2
3
12
23
34
123
245 etc .

Can anyone help me to this?
Posted
Updated 30-Jun-22 7:26am
Comments
MarqW 20-Mar-14 6:28am    
Check your datatype. You might have it as a String, in which case, sorting would be correct; make sure it's numeric (integer etc.)

1 solution

You can create DataTable via Clone, and change the data type of the column to Int.

DataTable dt = GetTable(); // Assume this method returns the datatable from service
DataTable dt2 = dt.Clone();
dt2.Columns["Code"].DataType = Type.GetType("System.Int32");

foreach (DataRow dr in dt.Rows)
{
dt2.ImportRow(dr);
}
dt2.AcceptChanges();
DataView dv = dt2.DefaultView;
dv.Sort = "Code ASC";
 
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