|
tech
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Sorting a Datagrid containing Dash / HyphenI'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/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 > It sorts just as Strings. And the hyphen is the decimal value 45 the zero I'm not sure that's true...48 > and that until the 9 which is decimal value 57. The tilde has the decimal > value 126. So there is nothing strange. 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[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 > A solution, I cannot find something either. Well thanks a lot for trying anyway...> Cheers, Chris. ....ANYONE ELSE???
Other interesting topics
Uploading file to a different web platform using ASP.NET/VB.NET
use of SqlDataAdapter with a table without Primary Key XML combo problems How do I validate null data now that isnull has gone? steps to call a web service method from vbscript... Closing file closes Visual studio .NET Sorting a datagrid with Hyphen / Dash Combo XML problems Compact Framework .NET V2 & WebBrowser Control Week view of the calendar control |
|||||||||||||||||||||||