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?
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.
Regards,
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
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.
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?
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.