I have a datatable (.Net) with multiple columns. One of the columns say RollNo is of string type but contains numeric data. Eg. 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14. I am trying to sort using the following:
string sql =
"Select StudentID, RollNo, AdmissionID,(FirstName + Space(1) + Isnull(MiddleName,'') + Space(1) + Isnull(LastName,'')) as Name," +
" PermState as State from Students where ClassId = '" + ddlClass.SelectedValue + "'" +
" order by RollNo";
DataTable dt = bl.GetDataSet(sql);
dt.DefaultView.Sort = "RollNo";
But after sorting I get the result as 1, 10, 11, 12, 13, 14, 2, 3, 4, 5, 6, 7, 8, 9.
How to solve it?
You can use
Linq-To-DataSetand cast thestringtoint:You need to add a reference to
System.Data.DataSetExtensions.dllandusing System.Linq;Note that you can omit the
CopyToDataTableif you don’t need it, i’ve showed it just to demonstrate how to get aDataTablefrom theIEnumerable<DataRow>.