471,344 Members | 1,334 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,344 software developers and data experts.

Sorting a Datagrid containing Dash / Hyphen

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...
Jul 21 '05 #1
4 2559
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/de...es_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
Jul 21 '05 #2
> 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.

Jul 21 '05 #3
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

Jul 21 '05 #4
> A solution, I cannot find something either.

Well thanks a lot for trying anyway...

Cheers,

Chris.
....ANYONE ELSE???
Jul 21 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Phil Powell | last post: by
reply views Thread by Chris Mayers via .NET 247 | last post: by
4 posts views Thread by Chris Mayers | last post: by
8 posts views Thread by simchajoy2000 | last post: by
1 post views Thread by mats.broberg | last post: by
4 posts views Thread by deancarstens | last post: by
reply views Thread by =?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?= | last post: by
5 posts views Thread by jrod11 | last post: by
reply views Thread by Ronak mishra | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.