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
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); } }
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,
You can set SortIndicator Property in initialize grid event like this:
grid.DisplayLayout.Bands[0].Columns["YourColumn"].SortIndicator = SortIndicator.Descending;
Alex.