The easiest way to sort the data as integer/number in Grid using datatable
If you have numeric codes stored in a DataTable. When You try to sort it the column take as string.
The grid displays below as ascending order(string sorting)
55 77 8
But I need the columns to be displayed like this(Integer sorting)
But I need the columns to be displayed like this(Integer sorting)
8 55 77
If we are using Datatable then specify the type of the field while adding the column else the field will be of type string.
DataTable dt= new DataTable();
dt.Columns.Add("ID", typeof(int));
then
DataView dv = new DataView(dt);
dv.Sort = "ID desc";
DataTable sortedDT = dv.ToTable();
datagrid.DataSource = sortedDT;
datagrid.DataBind();
If we are using Datatable then specify the type of the field while adding the column else the field will be of type string.
DataTable dt= new DataTable();
dt.Columns.Add("ID", typeof(int));
then
DataView dv = new DataView(dt);
dv.Sort = "ID desc";
DataTable sortedDT = dv.ToTable();
datagrid.DataSource = sortedDT;
datagrid.DataBind();
Comments
Post a Comment