Still having problem
Dear All.
I have a number sorting problem in datagrid.
I have integer values (1,2,11) , when i click Datagrid Header to sort the data , the result shows like this :
1
11
2
This is because the it is sorting as string. Can anybody help me how to sort it as a number.
Thanks
If you sort on your datasource then it will work. The DataGrid sorting code just uses strings in its default sort.
-mark
Program Manager
Microsoft
This post is provided "as-is"
No Sir, this does not work. Please check the code below , i tried as you said.
| | DataTable table = new DataTable("USERS"); DataColumnCollection columns = table.Columns; DataColumn column; column = columns.Add("USER_ID", typeof(System.String)); column.AllowDBNull = false; DataRow row = table.NewRow(); row["USER_ID"] = "1"; table.Rows.Add(row); row = table.NewRow(); row["USER_ID"] = "2"; table.Rows.Add(row); row = table.NewRow(); row["USER_ID"] = "11"; table.Rows.Add(row); dg.DataSource=table; |
Can u please check it.
Thanks.
Hi,
Use a DataView to sort data in a DataTable Object... And set the dataview to the datasource of the DataGrid Control...
eg.
| | DataView myView = new DataView(table) myView.Sort = "User_ID ASC" '' ASC - ascending, DESC- descending dg.DataSource = myView dg.DataSource=table;
|
cheers,
Paul June A. Domag
Hi, It is still not working, if you reorder the entry, then it is now working. Here is the code.:
DataTable table =
new DataTable("USERS");
DataColumnCollection columns = table.Columns;
DataColumn column;
column = columns.Add("USER_ID", typeof(System.String));
column.AllowDBNull = false;
DataRow row = table.NewRow();
row["USER_ID"] = "1";
table.Rows.Add(row);
row = table.NewRow();
row["USER_ID"] = "11";
table.Rows.Add(row);
row = table.NewRow();
row["USER_ID"] = "2";
table.Rows.Add(row);
DataView myView = new DataView(table);
myView.Sort = "User_ID ASC";
dg.DataSource = myView;
dg.DataSource=table;Thanks