Sorting a Datagrid containing Dash / Hyphen

  • Thread starter Thread starter Chris Mayers
  • Start date Start date
C

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...
 
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/d...l/_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
 
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.
 
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[0] = str;}
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
 
A solution, I cannot find something either.

Well thanks a lot for trying anyway...

Cheers,

Chris.


....ANYONE ELSE???
 
Ann said:
Hey chris,

Did you get the solution to this problem. I am having same problem now.

Can you please help?

From
http://www.google.com/search?hl=en&q=defaultView.sort+ignores+hyphen&aq=f&oq=

Posted via DevelopmentNow.com Groups
http://www.developmentnow.com/g/


That thread is about four years old. I believe if the default sort does not
work as you wish, you need to look at DataGridViewColumn.SortMod being set
to Programatic. You then need to handle sorting on your own.
 
Back
Top