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

[297 byte] By [ImranHaider] at [2008-2-7]
# 1
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"

MarkRideout at 2007-8-21 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 2

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.

ImranHaider at 2007-8-21 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 3
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

PaulDomag at 2007-8-21 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 4
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

ImranHaider at 2007-8-21 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 5
Hi,
I think the problem here is you've set the datasource first to the DataView:
dg.DataSource = myView;
Then set it again in the table:
dg.DataSource = table;

I think omitting the dg.DataSource = table; code would do the trick...
cheers,
Paul June A. Domag
PaulDomag at 2007-8-21 > top of Msdn Tech,Windows Forms,Windows Forms General...