Link to home
Start Free TrialLog in
Avatar of rrr333222
rrr333222

asked on

DataGridView Sorting?

I have a column of numbers in a DataGridView but when Sortign by clicking on the Column Header it sorts it as it were Text. How can I get it to sort as a value not as text?
Avatar of nickhoggard
nickhoggard

Hi,

What sort of datasource are you using for your datagridview?  What is the datatype for the column being sorted?

Generally the BindingSource or DataSource is responsible for performing the sort.  Therefore the fix to your problem will depend on what sort of dataset you are using.

Cheers

Nick
Avatar of rrr333222

ASKER

I am filling the DataGridView manualy in code. And I tried both putting in a string and a VAL if make no difference, in both cases it sorts  it as it were a string. I even set the format of this column as Numeric, with null being 0.
ASKER CERTIFIED SOLUTION
Avatar of Mystify
Mystify

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks
Great solution.
I'm trying to do the same thing, and your solution works like a dream for 1, 2, 3, 11, 12 and getting them in logical order.  When I have a combination of alphanumeric and numeric though it throws an error.

My cells have:  A01, A02, A03, 1, 2, 3, 11, 12

This line:     e.SortResult = CompareEx(e.CellValue1.ToString, e.CellValue2.ToString)

Is saying:  Object reference not set to instance of an object.