Datagrid number sorting

  • Thread starter Thread starter Mike
  • Start date Start date
M

Mike

I'm passing a number to a datagrid but when I sort on that column its
sorting it as a string, not a number. eg 100 comes before 2.

eg
//Get Key from an array of objects
.....
dr["Instrument Key"] = System.Convert.ToDecimal(RatesArray[i,4]);
.....

DataGridTextBoxColumn instrumentKeyColumn = new
DataGridTextBoxColumn();
instrumentKeyColumn.MappingName="Instrument Key";
instrumentKeyColumn.HeaderText = "Instrument Key";
instrumentKeyColumn.Width = 80;
instrumentKeyColumn.Alignment = HorizontalAlignment.Right;
//instrumentKeyColumn.Format="n0";
dgts.GridColumnStyles.Add(instrumentKeyColumn);
.....

Any idea what I'm doing wrong?
 
You need to ensure the datagrid knows its a number. The best way I think to
do things like this is to make a datatable with the column of the right type.
Then you can bind the grid to the datatable and do sorting and filtering on
the table.

Ciaran O'Donnell
 
Thanks for the idea but I'm not going to create another in memory copy
of my data just to do that, the data set in the array is already big
enough.

I guess the users will just have to live with an invalid sort or pay
for a better data grid control if the standard one is not smart enough
to know its being passed a number instead of a string.

Mike

You need to ensure the datagrid knows its a number. The best way I think to
do things like this is to make a datatable with the column of the right type.
Then you can bind the grid to the datatable and do sorting and filtering on
the table.

Ciaran O'Donnell

Mike said:
I'm passing a number to a datagrid but when I sort on that column its
sorting it as a string, not a number. eg 100 comes before 2.

eg
//Get Key from an array of objects
.....
dr["Instrument Key"] = System.Convert.ToDecimal(RatesArray[i,4]);
.....

DataGridTextBoxColumn instrumentKeyColumn = new
DataGridTextBoxColumn();
instrumentKeyColumn.MappingName="Instrument Key";
instrumentKeyColumn.HeaderText = "Instrument Key";
instrumentKeyColumn.Width = 80;
instrumentKeyColumn.Alignment = HorizontalAlignment.Right;
//instrumentKeyColumn.Format="n0";
dgts.GridColumnStyles.Add(instrumentKeyColumn);
.....

Any idea what I'm doing wrong?
 
Back
Top