Hi,
We allow our users to select columns by which to group a grid. We need to save their choice so as to be able to re-instate it later (automatically).
Could you please let me know how to programmatically (C#) retrieve columns by which a grid is currently grouped, and also how to programmatically group a grid by some columns? We use WinForms UltraWinGrid and WPF XamGrid, and I would really appreciate if you could provide code samples for both of them.
Thank you.
Best regards,
Adam
Hi Adam,
In that case, my best guess is that this is a threading issue. Is your application using multiple threads?
Hi Mike,
The exception occurs intermittently. Sometimes SortedColumnsCollection.Add does not generate any exceptions, and we always call it in the same line of code.
Here is the stack trace:
at Infragistics.Win.UltraWinGrid.RowsCollection.EnsureFiltersEvaluated() at Infragistics.Win.UltraWinGrid.RowsCollection.EnsureSortedAndFiltered(RecursionType recursion, UltraGridBand lowestLevelBand) at Infragistics.Win.UltraWinGrid.RowsCollection.EnsureSortedAndFiltered(RecursionType recursion, UltraGridBand lowestLevelBand) at Infragistics.Win.UltraWinGrid.RowsCollection.EnsureSortedAndFilteredHelper(ProcessMode processMode, UltraGridBand lowestLevelBand) at Infragistics.Win.UltraWinGrid.SortedColumnsCollection.ProcessNewSortedColumnsHelper(SortedColumnsCollection newSortedColumns, Boolean fireEvents, Boolean groupBy) 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(String key, Boolean descending, Boolean groupBy)
We use Infragistics v13.1.
On what line of code are you getting this exception? What's the call stack look like?
This sounds to me like your code must be trying to cast a data row into a GroupByRow and it's not a GroupByRow. Or, it could be a bug in an older version of the grid.
Thank you for the reply. I tried the solution suggested by you and it works fine if there is a single GroupBy column. However, with two GroupBy columns I keep getting the following exception:
Unable to cast object of type 'Infragistics.Win.UltraWinGrid.UltraGridRow' to type 'Infragistics.Win.UltraWinGrid.UltraGridGroupByRow'
I catch the exception and the grid does end up showing two GroupBy columns. I tried enforcing execution on the UI thread (grid.Invoke) but the exception persisted. Should I just ignore it?
I don't know anything about the XamGrid. But for the WinForms grid, grouping is tightly tied to sorting. So what you do is loop through the SortedColumns collection on the band and check the IsGroupBy property on the column.
To group a column, you use the SortedColumns.Add method. Use the overload that takes three parameters. The last param is a bool which indicates whether to group by the column.