Hi, I have a grid with custom column comparer class for each column to do custom sorting. Right now, I can do one column sorting, or multiple column sorting by holding shift key and select multiple columns. However, we want to always add the first column for sort. User wants to just click one of the other column to perform two columns sorting without holding the shift key. In other word, we want to add the first column to sorting columns as the first sort no matter which column user clicks.
One possible way to do that, I think, is to disable the column click sorting on column header, catch the header click event, then programatically add the first column and the column user click to sorted column list.
Is there any easy way to do that?
Your custom comparer can take any criteria into consideration, so all you have to do is factor in the value of the cell in the first column. The Compare method gets passed two cells (one from each of the rows being compared), so you can get a reference to the row off each cell and get the value of the first column cell in each row and make that your primary sort criteria.
thanks for your reply. However, the problem is that we have to make the change to every comparer class and those comparer classes are also shared with other places which doesn't not need this extra logic. I am wondering whether WinGrid is capable of locking the first sort always such that no matter what column header user click, the first column is always fixed and the grid will just add whatever user clicks to the second column and so forth.
thanks,
zongwen
Hi,
If you already have IComparer classes for your data, it would be a simple matter to create another IComparer for the grid that simply wraps the existing IComparer. So your Compare method implementation would get the cells and compare the values in the first sort you want and then if the values are equal, call off to your other IComparer class.
Another option is for you to handle the BeforeSortChanged event of the grid and always cancel the event, then add to the SortedColumns collection of the band yourself. So you would always add the first column you want and then add in the columns the customer sorted by after that.
I guess I was not clear.
Do not try to modify grid.DisplayLayout.Bands[0].SortedColumns. Instead, modify e.SortedColumns.
I tried this out and it works fine for me.
I removed the e.Cancel = true, but still gives me the same exception. still can't modify the SortedColumn collection.
Thanks,
Zongwen
Hi Zongwen,
Sorry about that. It looks like the grid will not allow you to directly change the SortedColumns on the band once you are inside that event.
What you can do is... don't cancel the event. Just modify the e.SortedColumns collection so that the column you want is always the first item in the collection.
Thanks Mike.
Here is what I get when I try to cancel the event and modify SortedColumns collection inside BeforeSortChange event:
************** Exception Text **************
System.ArgumentException: Can't modify the sorted columns collection while in the BeforeSortChange event
at Infragistics.Win.UltraWinGrid.SortedColumnsCollection.SetSortedColumn(UltraGridColumn column, SortIndicator sortIndicator, Boolean groupBy, Boolean clearExistingNonGroupByColumns, Boolean fireEvents)
at Infragistics.Win.UltraWinGrid.SortedColumnsCollection.Add(UltraGridColumn column, Boolean descending, Boolean groupBy)
at Infragistics.Win.UltraWinGrid.SortedColumnsCollection.Add(UltraGridColumn column, Boolean descending)
at InteractiveDataFIA.BondEdge.ReportMultiPortfolio.grid_BeforeSortChange(Object source, BeforeSortChangeEventArgs e) in D:\benet3-25\benet\ReportMultiPortfolio.cs:line 2549
at Infragistics.Win.UltraWinGrid.BeforeSortChangeEventHandler.Invoke(Object sender, BeforeSortChangeEventArgs e)
at Infragistics.Win.UltraWinGrid.UltraGridBase.OnBeforeSortChange(BeforeSortChangeEventArgs e)
at Infragistics.Win.UltraWinGrid.UltraGrid.FireEvent(GridEventIds id, EventArgs e)
at Infragistics.Win.UltraWinGrid.UltraGrid.FireCommonEvent(CommonEventIds id, EventArgs e, Boolean checkInProgress)
at Infragistics.Win.UltraWinGrid.SortedColumnsCollection.ProcessNewSortedColumnsHelper(SortedColumnsCollection newSortedColumns, Boolean fireEvents, Boolean groupBy)
at Infragistics.Win.UltraWinGrid.UltraGridBand.SetSortedColumn(UltraGridColumn column, SortIndicator sortIndicator, Boolean clearExisting)
==
Here is my event handler:
---
private void grid_BeforeSortChange(object source, BeforeSortChangeEventArgs e)
{
CmsRichGrid grid = source as CmsRichGrid;
if (grid == null)
return;
List<UltraGridColumn> sortedColumns = new List<UltraGridColumn>();
bool includePortfolioColumn = false;
foreach (UltraGridColumn col in e.SortedColumns)
sortedColumns.Add(col);
if(col.Key == displayPortfolioNameField)
includePortfolioColumn = true;
}
e.Cancel = true;
if (!includePortfolioColumn)
sortedColumns.Insert(0, grid.DisplayLayout.Bands[0].Columns[displayPortfolioNameField]);
grid.BeforeSortChange -= new BeforeSortChangeEventHandler(grid_BeforeSortChange);
foreach(UltraGridColumn col in sortedColumns)
grid.DisplayLayout.Bands[0].SortedColumns.Add(col, false);
grid.BeforeSortChange += new BeforeSortChangeEventHandler(grid_BeforeSortChange);