I'm trying to sort a datatable in a datagrid by a column with a string value. There are certain items I would like towards the bottom of the list that would normally have a {null} value in the string column. A quick fix was to populate them with a character value higher than any alphabetic character. That way they would always be sorted last. However, I ran into what I consider very odd behavior. The character (a "~") sorted first?!?!?
Could someone elaborate as to how a dataview performs a sort on a string?
To see the odd behavior I describe create a form and drop ina datagrid (called "dataGrid1"). Drop this into the form's load event:
DataTable dt = new DataTable("test");
dt.Columns.Add("char",Type.GetType("System.String"));
dt.Rows.Add(new object[] {"Z"});
dt.Rows.Add(new object[] {"V"});
dt.Rows.Add(new object[] {"\u007B"});
dt.Rows.Add(new object[] {"A"});
dt.Rows.Add(new object[] {"\u007C"});
dt.Rows.Add(new object[] {"\u007D"});
dt.Rows.Add(new object[] {"\u007E"});
dt.Rows.Add(new object[] {"\u005A"});
dt.Rows.Add(new object[] {"\u0041"});
DataView dv = new DataView(dt,"","char ASC",DataViewRowState.CurrentRows);
dataGrid1.DataSource = dv;
What is going on here? I thought the sort was done on Unicode values?
Could someone elaborate as to how a dataview performs a sort on a string?
To see the odd behavior I describe create a form and drop ina datagrid (called "dataGrid1"). Drop this into the form's load event:
DataTable dt = new DataTable("test");
dt.Columns.Add("char",Type.GetType("System.String"));
dt.Rows.Add(new object[] {"Z"});
dt.Rows.Add(new object[] {"V"});
dt.Rows.Add(new object[] {"\u007B"});
dt.Rows.Add(new object[] {"A"});
dt.Rows.Add(new object[] {"\u007C"});
dt.Rows.Add(new object[] {"\u007D"});
dt.Rows.Add(new object[] {"\u007E"});
dt.Rows.Add(new object[] {"\u005A"});
dt.Rows.Add(new object[] {"\u0041"});
DataView dv = new DataView(dt,"","char ASC",DataViewRowState.CurrentRows);
dataGrid1.DataSource = dv;
What is going on here? I thought the sort was done on Unicode values?