Hi,
Is there any way of changing the direction that a column initialy sorts in? Currently it sems that the UltraGrid will sort in Ascending order first, but we would prefer this to be descending.
I have tried changing the SortIndicator property of all columns that are not in the current sorted set in the BeforeSortChange event. However when I do this I recieve an Argument exception saying its its not possible to modify the collection in this event.
Thanks
Chris
You can set SortIndicator Property in initialize grid event like this:
grid.DisplayLayout.Bands[0].Columns["YourColumn"].SortIndicator = SortIndicator.Descending;
Alex.
Sorry - to be more clear.
When a grid column header is clicked, and the sort indicator changes from None (unsorted) to a direction. For the first click the direction is Ascending, and to get a Descending order the user must click again.
In our application it would make more sense to have this default to Ascending first for some columns, the line above will initally set a column to be sorted descening, but this isn't quite what we require here.
Thanks for the suggestion,
Hi Chris,
This was a bit tricky, but I got it to work. Here's some sample code:
delegate void SortColumnDescendingDelegate(UltraGridColumn column); private void ultraGrid1_BeforeSortChange(object sender, BeforeSortChangeEventArgs e) { // Loop through the new SortedColumns and see if there is a new column in there // that did not exist in the original sorted columns. This will tell us if a // column is being sorted for the first time. foreach (UltraGridColumn column in e.SortedColumns) { if (e.Band.SortedColumns.Exists(column.Key) == false) { // We found a new column. Cancel the change. e.Cancel = true; // Now we want to sort the column descending. But we cannot change the // SortedColumns collection inside this event. So we will do it // by invoking another method. this.BeginInvoke(new SortColumnDescendingDelegate(this.SortColumnDescending), new object[ { e.Band.Columns[column.Key] }); } } } private void SortColumnDescending(UltraGridColumn column) { // Disable the BeforeSortChange event to avoid recursion. this.ultraGrid1.EventManager.SetEnabled(GridEventIds.BeforeSortChange, false); try { // Sort the specific column descending. column.Band.SortedColumns.Add(column, true); } finally { //Re-enabled the BeforeSortChange event. this.ultraGrid1.EventManager.SetEnabled(GridEventIds.BeforeSortChange, true); } }
Hi Mike, thank you for look into it. We could like to have multi column sort. I use a comparer class for my image columns to change sort value to make it work.
Thanks,
Crystal.
Hi Crystal,
I tried out my sample code above and I do indeed get some weird results when I click on a second column header. It looks like my SortColumnDescending code was not quite complete. I am adding the new sorted column, but I never cleared the old one. So the grid ends up sorting both columns descending, but since the first sorted column contains unique values, the second sort doesn't do anything.
If you only want a single-column sort, then you can do this:
private void SortColumnDescending(UltraGridColumn column) { // Disable the BeforeSortChange event to avoid recursion. this.ultraGrid1.EventManager.SetEnabled(GridEventIds.BeforeSortChange, false); try { // Sort the specific column descending. column.Band.SortedColumns.Clear(); column.Band.SortedColumns.Add(column, true); } finally { //Re-enabled the BeforeSortChange event. this.ultraGrid1.EventManager.SetEnabled(GridEventIds.BeforeSortChange, true); } }
All I did was add the call to SortedColumns.Clear before adding the new column.
Hi Mike, we have similar requirement that needs to sort some image columns descending when first click on the column header. Your approach works first time click on a column. But when I click on another column header, that column wasn't sorted by descending, however, SortColumnDescending method was called though.