C1DataGrid - MergingHelper - Merged ColumnHeaders

Posted by: random123 on 19 September 2017, 10:51 am EST

  • Posted 19 September 2017, 10:51 am EST

    Hi.

    I am currently trying to implement merged column headers in WPF with the help of the Silverlight example found here: http://demo.componentone.com/silverlight/controlexplorer/#DataGrid/Headers%20Merging

    My requirement is that only 2 columns are being mereged and the others should have no merging applied. And there lies my problem.

    Lets assume I have 6 columns, the column 1, 2, 5, 6 are supposed to be “normal” columns.

    Column 3, 4 are supposed to have merged headers.

    After implementing the Silverlight example in my WPF application I get the following result:

    Column 1, 2 are merged vertically and text is in the center.

    Column 3, 4 have the first row merged horizontal and the 2nd row is unmerged.

    Everything till now is like it should be, but now…

    Column 5, 6 are unmerged neither horizontally, nor vertically so this column show up with doulbe text in there headers.

    Hope the following helps to illustrate the problem:

                              Col 3 - Col 4       Col 5     Col 6
    

    Col1 Col 2

    Col 3 Col 4 Col 5 Col 6

    Columns 5, 6 should also be merged vertically and the text aligned in center, so my desired result should be this:

                              Col 3 - Col 4
    

    Col1 Col 2 Col 5 Col 6

    Col 3 Col 4

    The problem always occurs to columns that come after the merged columns, so if I would change the postion from columns 3, 4 to be at the very end all the previous columns would work like they should.

    On the other side would I move them to the very front all following columns would be broken and unmerged.

    After some digging around I may found the problem and want to share my finding with you, if its correct.

    In the MergingHelper.cs from the Silverlight Example you will find the following code:

    [csharp]

    innerMerges = (orientation == Orientation.Vertical)

    ? Merge(orientation, range.Rows.ToArray(), pendingColumns, hierarchical)

    : Merge(orientation, pendingRows, range.Columns.ToArray(), hierarchical);

    [/csharp]

    I modified it to this code:

    [csharp]innerMerges = (orientation == Orientation.Vertical)

    ? Merge(Orientation.Horizontal, range.Rows.ToArray(), pendingColumns, hierarchical)

    : Merge(Orientation.Vertical, pendingRows, range.Columns.ToArray(), hierarchical);[/csharp]

    With this modification my problem is gone.

    Will I face other problems with my modification or is it a “good” one?

  • Posted 19 September 2017, 10:51 am EST

    Hi,

    MergingHelper.cs is anyway a custom class that was created to implement merging logic within Datagrid. So yes this should work and you shouldn’t face any issues with your custom logic. If you do, please let me know.

    Moreover, if you simply wish to merge some specific columns’ headers, then I don’t think you really need to use MergingHelper. Rather, simple merging of CellRanges with ‘MergingCells’ event of the grid should do this for you.

    Something like this:

    [csharp]

    c1DataGrid1.MergingCells += (s, e) =>

    {

    var _grid = s as C1DataGrid;

    var _mergeList = new List<DataGridCellsRange>();

    _mergeList.Add(new DataGridCellsRange(_grid.GetCell(0, 0), _grid.GetCell(1, 0))); _mergeList.Add(new DataGridCellsRange(_grid.GetCell(0, 1), _grid.GetCell(1, 1)));

    _mergeList.Add(new DataGridCellsRange(_grid.GetCell(0, 2), _grid.GetCell(1, 2)));

    _mergeList.Add(new DataGridCellsRange(_grid.GetCell(0, 3), _grid.GetCell(0, 5)));

    _mergeList.Add(new DataGridCellsRange(_grid.GetCell(0, 6), _grid.GetCell(0, 8)));

    _mergeList.Add(new DataGridCellsRange(_grid.GetCell(0, 9), _grid.GetCell(1, 9)));

    _mergeList.Add(new DataGridCellsRange(_grid.GetCell(0, 10), _grid.GetCell(1, 10)));

    foreach (var range in _mergeList)

    {

    e.Merge(range);

    }

    };

    [/csharp]

    Here’s some logic to center align text in column header cell:

    [csharp]

    c1DataGrid1.LoadedCellPresenter += (s, e) =>

    {

    if (e.Cell.Presenter.Content is DataGridColumnHeaderPresenter)

    {

    var cc = (e.Cell.Presenter.Content as ContentControl);

    var tb = cc.Content as TextBlock;

    tb.TextAlignment = TextAlignment.Center;

    tb.HorizontalAlignment = HorizontalAlignment.Center;

    }

    };

    [/csharp]

    ~Anupam.

  • Posted 19 September 2017, 10:51 am EST

    Thanks for the fast and very nice answer.

  • Posted 22 May 2018, 12:09 am EST

    I am using merge cell sample of C1 for wpf

    But have a bug relate to width of merged cell, length of header content smaller display area but it show “…” instead of content of header

    Anyone has solution to solve this problem? thanks a lot

  • Posted 2 July 2018, 10:32 pm EST

Need extra support?

Upgrade your support plan and get personal unlimited phone support with our customer engagement team

Learn More

Forum Channels