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,
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.
Hi Mike,
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?
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.
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.
In that case, my best guess is that this is a threading issue. Is your application using multiple threads?
Mike,
I am happy with the solution as it is. As long as I capture the exception (and ignore it) the final layout, including grouping columns, is correct.
Thanks for your help.
Regards,
Well, if you are not using threading, then I'm afraid I can't explain the exception. Can you post a small sample project demonstrating this exception so we can check it out and debug it?
We do everything on the UI thread. The call to Invoke is redundant, I added it to to check if the exception still occurs. The exception happens intermittently when there are more than one GroupBy columns. For a single GroupBy column no exception occurs.
This won't work. Using an Invoke or BeginInvoke is good if you can do it all the time whenever information is passed across threads. But that's not the case here. You are not in control of the communication between the grid, the DataSource, and the BindingManager. So you cannot possibly do all of the proper marshalling that would be required in order to deal with the grid (or any bound control) while it is using DataBinding. DataBinding and threading just don't mix, except under some very rare and limited circumstances. There's a long, detailed discussion of this here:
Work with a dataset bound to a grid on a separate thread - Infragistics Community
Yes, we do use multi-threading. I did try to use grid.Invoke to explicitly execute the code on the UI thread but the problem persisted.