I need to merge two column headers that are side-by-side into a single column header. How do I do that? I'd like the columns themselves to remain separate. I just want the header to look like a single header.
Hi,
There's no way to do this via simple property settings.
But there are a couple of ways you can achieve what you want. It's going to depend on what version of the controls you are using, though.
If you have a pretty recent version, then you could set the RowLayoutStyle on your grid to GroupLayout. Then you put the two columns into a group and hide the columns headers so that the group header takes the place of the column headers.
If you have an old version of the controls, then you may not have the option for GroupLayout, so you could use a RowLayout with an unbound column. You use the column.RowLayoutColumnInfo.LabelPosition to hide the column headers for the two real bound columns and hide the cells (LabelOnly) for the unbound column. The unbound column's header will act as the combined header for the two real columns.
A third option, which will work in any version of the grid but is a bit harder to implement it to use a CreationFilter to remove the UIElement for one of the column headers and extend the other into the empty space thus created.
This will be tricky, though, especially if you are not experienced with CreationFilters.
In the 1st method, what is the code to hide the column header under the group.
Hope this column hiding you specified only hides the header under the group and not its cells below it.
Thank you,
Hi Mike,
Can i get a working sample of 1st and 2nd option.
Thank you.
Mike, I'm pursuing the CreationFilter method, but I'm a bit confused. The code is below.
My problem is that BeforeCreateChildElements() gets called multiple times for the same headers without resetting the header rectangle to its original values and because of this, the code that calculates the rect, by adding the width of the adjacent column, continues to add that width over and over and over again, resulting in a column header that's ridiculously wide.
I can't seem to determine when it's getting called for the first time vs. the 5th time. How can I fix it so it only gets called once, or at least, only calculates the new header width once.
Thanks
public class ColHeaderSpannerCreationFilter : IUIElementCreationFilter { private string ColumnKeyMain { get; set; } private string ColumnKeyHidden { get; set; } private UltraGrid Grid { get; set; } public ColHeaderSpannerCreationFilter(UltraGrid grid, string colKeyMain, string colKeyHidden) { Grid = grid; ColumnKeyMain = colKeyMain; ColumnKeyHidden = colKeyHidden; } public void AfterCreateChildElements(UIElement parent) { } public bool BeforeCreateChildElements(UIElement parent) { if (parent is HeaderUIElement) { //Get a reference to the cell, and the cells ui element. HeaderUIElement headerUIElement = (HeaderUIElement)parent; UltraGridColumn headerCell = headerUIElement.Header.Column; // Left-hand column if (headerCell.Key == ColumnKeyMain) { UltraGridColumn rightCol = Grid.DisplayLayout.Bands[0].Columns[ColumnKeyHidden]; //This is a cell that should span two columns, so we stretch its rectangle. headerUIElement.Rect = new Rectangle(headerUIElement.Rect.X, headerUIElement.Rect.Y, headerUIElement.Rect.Width + rightCol.Width, headerUIElement.Rect.Height); return true; } // Right-hand column if (headerCell.Key == ColumnKeyHidden) { //Here we are hiding the cell by shrinking its height and width down to 0. headerUIElement.Rect = new Rectangle(headerUIElement.Rect.X, headerUIElement.Rect.Y, 0, 0); //Return true to let the grid know that we handled the event. return true; } } //Return false to let the grid know we did not handle the event. return false; } }
I actually considered the group idea originally, but it seemed a bit kludgy. I'll look at the creation filters. Thanks.