Home All Groups Group Topic Archive Search About

Sorting a Datagrid containing Dash / Hyphen

Author
8 Mar 2005 5:39 PM
Chris Mayers
Hi,

I'm trying to sort a DataGrid (dotNet Windows Forms).
Trouble is, the column I'm trying to sort on contains part numbers of the
form
xxx-yyy where

xxx - Part Type
yyy - Part Size.

The DataGrid sorts into a sequence somthing like:

10-199
10-299
103-10
103-20
10-399

wheras I would like:
10-199
10-299
10-399
103-10
103-20

It seems that it ignores the '-' when sorting (trying to be helpful??).
Anyone got any ideas how I can get the DataGrid to sort using exactly what
is in the column rather than applying any other 'rules' to it.

If I replace the '-' s with '~'s then it sorts fine, but that is not really
a solution.

Look forward to hearing any suggestions.

(I haven't included any details on the code or the data source as they don't
seem to make any difference, but if anyone things it is relevent, I'm happy
to supply them.)

Thanks,

Chris.

PS Apologies if the post appears more than once, but I originally tried to
post it via dotNET247 and after 3 hours it still isn't here...

Author
8 Mar 2005 5:57 PM
Cor Ligthert
Chris,

It sorts just as Strings. And the hyphen is the decimal value 45 the zero 48
and that until the 9 which is decimal value 57. The tilde has the decimal
value 126. So there is nothing strange.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vsintro7/html/_pluslang_ascii_character_codes_chart_1.asp

What you think as a number is a string of characters that is sorted.
When you set the sort of the datagrid to disable, than you can think about
using an extra datacolumn with an expression, a dataview and datagridstyles.
Otherwise there are probably not much possibilities.

I hope this gives some idea's

Cor
Are all your drivers up to date? click for free checkup

Author
9 Mar 2005 9:41 AM
Chris Mayers
> It sorts just as Strings. And the hyphen is the decimal value 45 the zero
48
> and that until the 9 which is decimal value 57. The tilde has the decimal
> value 126. So there is nothing strange.

I'm not sure that's true...

Like I said, the sort order is:
10-199
10-299
103-10
103-20
10-399

If  it was doing a simple ASCII sort, '10-...' should come before '103...'
so the sequence is surely not plain ascii sorted. However if you ignore the
'-'s, the order is

10199
10299
10310
10320
10399

Which makes sense. This is why I think it is ignoring the '-' when doing the
sort.
I figure if you were sorting normal text, you would probably want (say)
'cooperation' to be sorted next to 'co-operation'  so this behaviour would
be correct, however in this situation is is not. Maybe there is a flag to
chose the sort behaviour??

Regards,

Chris.
Author
9 Mar 2005 12:20 PM
Cor Ligthert
Chris,

I think you are completly right in all what you wrote, in my idea should
this be called a worse Bug

I made something to test the behaviour.

\\\
System.Data.DataTable dt = new System.Data.DataTable();
dt.Columns.Add("Chris",Type.GetType("System.String"));
string[] str = new string[]
{"10-199","10-299","10-399","103-10","103-20"};
for (int i = 0;i < str.Length;i++){
dt.Rows.Add(dt.NewRow());
dt.Rows[i][0] = str[i];}
dt.DefaultView.Sort = "Chris ASC";
listBox1.DataSource = dt.DefaultView;
listBox1.DisplayMember = "Chris";
///

A solution, I cannot find something either.

Sorry, maybe somebody else has the solution.

Cor
Author
9 Mar 2005 5:38 PM
Chris Mayers
> A solution, I cannot find something either.
>


Well thanks a lot for trying anyway...

Cheers,

Chris.


....ANYONE ELSE???
Author
2 Jan 2009 3:23 PM
Ann
[Message not available]
Author
2 Jan 2009 9:55 PM
Family Tree Mike
[Message not available]

Bookmark and Share