I have a grid with 3 bands, but only the top band displays a column header. I want to allow the user to pin columns and have all three bands freeze. What events do i need to register to hook up the 2nd and 3rd band to the pinning of a column in the first band?
I was able to make it work, but it wasn't obvious. Because I have 3 bands, the columns need to stay in the same visible position. In fact, I attempt to prevent column swapping altogether. However, when a pin is attempted, the grid moves the pin column before all non-pinned columns. I could have synchronized the child bands to follow, but I decided to minimize user confusion that it was better to pin all columns to the left, instead of allowing the column to move.
Here is where it got a little confusing. The column collections are different for each band. For instance a parent band has a collection column that represents the child band elements. So, I couldn't simply loop through the columns looking at the column index. The next confusing thing is that the column collection is not reliably in the same order as the visible display order. So, looping the column collection was not reliable either. So, I created a list that predefined the visible order and used the list to get the columns in order by key. I move left-to-right when pinning, and right-to-left when un-pinning to prevent columns from moving.
I tried to consolidate the code below. I am sure it won't compile, but it will give you an idea. If you have a chance, please let me know if there is a better way to loop the visible columns in order.
if (grd.IsLoading) { e.Cancel = true; return; }
grd.EventManager.SetEnabled(GridEventIds.BeforeColPosChanged, false);
var band1 = grd.DisplayLayout.Bands[Bands.Band1];
var band3 = grd.DisplayLayout.Bands[Bands.Band3];
foreach (var column in e.ColumnHeaders) {
var columnIndex = band1.Columns[column.Column.Key].Header.VisiblePosition;
if (fixedColumn) {
column.Header.Fixed = column.Header.VisiblePosition <= columnModified;
}
. . . similar for other bands . . .
for (var idx = ColumnHeadings.Band1.VisibleColumns.Count - 1; idx >= 0; idx--)
band1.Columns[ColumnHeadings.Band1.VisibleColumns[idx]].Header.Fixed = false;
} e.Cancel = true; } finally { grd.EventManager.SetEnabled(GridEventIds.BeforeColPosChanged, true); }
public sealed class Band1 {
public static List<string> VisibleColumns { get { return new List<string> { CommonHeadings.Column1, . . .};}}}
I think you should use the AfterColPosChanged event. You will probably want to use the EventManager to disable the event while you are looping through the other bands and fixing their columns, so as to prevent recursion.