We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
close icon

Merge a set number of cells in a column at a time when having grouped columns

How do I merge 6 cells at a time in a GridGroupingControl?
I just can't figure out how to do this.

My scenario is as follows:
My DataSource is a DataTable.
In the DataTable I have x number of project categories set as grouped columns (expand/collapse).
In each category I have y number of projects.
Each project has 6 parts.
Each part takes up 25 cells.
Three of these cell columns should have merge enabled and the merge is to have 1*6 cells and be treated as a single cell (with the cells not just hidden beneath, but as a real, single, cell).
The projects can have the exacts same info (see my example below) but they should not be merged over projects (only in single project).

Example:


6 Replies

MG Mohanraj Gunasekaran Syncfusion Team February 17, 2017 12:50 PM UTC

Hi Henrick, 

Thanks for your interest in Syncfusion products. 

We could able to understand your scenario and created the simple sample as per your requirement. In order to merge the cells with same values while grouping, GroupedColumns.Changed event can be used. In that event, the cells with same values can be merged by setting the MergeCellsMode as either RowsInCoulumn and the EvaluateMergeCells method can be used to implement the merge cells. 
 
Code example: 
this.gridGroupingControl1.TableDescriptor.GroupedColumns.Changed += new Syncfusion.Collections.ListPropertyChangedEventHandler(GroupedColumns_Changed); 
 
void GroupedColumns_Changed(object sender, Syncfusion.Collections.ListPropertyChangedEventArgs e) 
    if (e.Action == Syncfusion.Collections.ListPropertyChangedType.Insert) 
    { 
        this.gridGroupingControl1.Table.ExpandAllRecords(); 
        //MergeCell 
        this.gridGroupingControl1.TableDescriptor.Columns["Project"].Appearance.AnyRecordFieldCell.MergeCell = GridMergeCellDirection.RowsInColumn; 
        this.gridGroupingControl1.TableDescriptor.Columns["MergeCol1"].Appearance.AnyRecordFieldCell.MergeCell = GridMergeCellDirection.RowsInColumn; 
        this.gridGroupingControl1.TableDescriptor.Columns["MergeCol2"].Appearance.AnyRecordFieldCell.MergeCell = GridMergeCellDirection.RowsInColumn; 
        this.gridGroupingControl1.TableDescriptor.Columns["MergeCol3"].Appearance.AnyRecordFieldCell.MergeCell = GridMergeCellDirection.RowsInColumn; 
        this.gridGroupingControl1.TableModel.Options.MergeCellsMode = GridMergeCellsMode.OnDemandCalculation | GridMergeCellsMode.MergeRowsInColumn; 
        this.gridGroupingControl1.TableModel.Options.MergeCellsLayout = GridMergeCellsLayout.Grid; 
        this.gridGroupingControl1.TableModel.MergeCells.EvaluateMergeCells(this.gridGroupingControl1.TableControl.GridCellsRange); 
 
        this.gridGroupingControl1.Refresh(); 
    } 
    //Clear the MergeedCells when ungroup the columns. 
    if (e.Action == Syncfusion.Collections.ListPropertyChangedType.Remove) 
    { 
        this.gridGroupingControl1.TableModel.Options.MergeCellsMode = GridMergeCellsMode.None; 
    } 
 
 
 
UG Document: 
 
Sample link: GridGroupingControl 
 
Note: 
When click on the merged cells, the current cell focus is not moved to top cell of that merged cells. So if you want to achieve that, add that merged cells to CoveredRanges. Please make use of the below customization, 
 
Code example: 
this.gridGroupingControl1.TableControlCellClick += new Syncfusion.Windows.Forms.Grid.Grouping.GridTableControlCellClickEventHandler(gridGroupingControl1_TableControlCellClick); 
void gridGroupingControl1_TableControlCellClick(object sender, Syncfusion.Windows.Forms.Grid.Grouping.GridTableControlCellClickEventArgs e) 
    GridRangeInfo range; 
    if (this.gridGroupingControl1.TableModel.GetSpannedRangeInfo(e.Inner.RowIndex, e.Inner.ColIndex, out range)) 
    { 
        GridRangeInfo info; 
        if (!this.gridGroupingControl1.TableModel.CoveredRanges.Find(e.Inner.RowIndex, e.Inner.ColIndex, out info)) 
            this.gridGroupingControl1.TableModel.CoveredRanges.Add(range); 
    } 
 
Regards, 
Mohanraj G. 
 



HL Henrik Lundqvist February 17, 2017 01:12 PM UTC

Sorry, that is not what I want to achieve. I already use ordinary merging in other parts of my program and it works as expected.
If you look at my example again, in this case I want to merge exactly 6 cells at a time that are alike (a Project in this case is build from 6 rows) - no more, no less.


MG Mohanraj Gunasekaran Syncfusion Team February 20, 2017 01:49 PM UTC

Hi Henrick, 

Sorry for the inconvenience caused. 

The GridGroupingControl also having the supports to add the covered ranges. It is the process of combining two or more adjacent cells, rows or columns and displayed with in a single cell. This can be achieved by adding the range of cells to the CoveredRanges collection. Please refer the below code example and refer the attached sample, 
 
Code example 
//Adding the range of cells into the covered range  
this.gridGroupingControl1.TableDescriptor.GroupedColumns.Changed += new Syncfusion.Collections.ListPropertyChangedEventHandler(GroupedColumns_Changed); 
 
void GroupedColumns_Changed(object sender, Syncfusion.Collections.ListPropertyChangedEventArgs e) 
{ 
    if (e.Action == Syncfusion.Collections.ListPropertyChangedType.Insert) 
    { 
        this.gridGroupingControl1.Table.ExpandAllGroups(); 
        for(int i=0;i<this.gridGroupingControl1.Table.Elements.Count;i++) 
        { 
            if (this.gridGroupingControl1.Table.Elements[i] is GridRecordRow) 
            { 
                GridRecordRow row = this.gridGroupingControl1.Table.Elements[i] as GridRecordRow; 
                for (int j = 0; j < row.ParentGroup.Groups.Count; j++) 
                { 
                    int toprowindex = row.ParentGroup.Groups[j].Records[0].GetRowIndex(); 
                    int count = row.ParentGroup.Groups[j].Records.Count; 
                    int bottomIndex = row.ParentGroup.Groups[j].Records[count-1].GetRowIndex(); 
                    this.gridGroupingControl1.TableModel.CoveredRanges.Add(GridRangeInfo.Cells(toprowindex, 2, bottomIndex, 2)); 
                    this.gridGroupingControl1.TableModel.CoveredRanges.Add(GridRangeInfo.Cells(toprowindex, 3, bottomIndex, 3)); 
                    this.gridGroupingControl1.TableModel.CoveredRanges.Add(GridRangeInfo.Cells(toprowindex, 4, bottomIndex, 4)); 
                    this.gridGroupingControl1.TableModel.CoveredRanges.Add(GridRangeInfo.Cells(toprowindex, 5, bottomIndex, 5)); 
                } 
            } 
        } 
   } 
 
} 
 
Sample link:  GridGroupingControl 
 
Please let us know if you have any concerns. 

Regards, 
Mohanraj G.   



HL Henrik Lundqvist February 21, 2017 03:26 PM UTC

Sorry, still not what I asked about - that is almost the same solution you provided before.

Please check the example image I provided before.

All projects belong to one of many category - the categories are the group.
Categories have several projects (not just one).
All projects take up exactly 6 rows (no exceptions).

The question (once again) is: I want to merge 6 cells at once. The cells are on 6 rows. No more, no less. How?

Example 2 (in text this time):

Category 1 - 2 projects - 12 rows
Project 1 (6 rows)
Project 2 (6 rows)

Category 2 - 3 projects - 18 rows
Project 3 (6 rows)
Project 4 (6 rows)
Project 5 (6 rows)

Category 3 - 1 project - 6 rows
Project 6 (6 rows)


HL Henrik Lundqvist February 22, 2017 11:18 AM UTC

Solved it myself when I managed to figure out that groups set through the TableDescriptor.GroupedColumns.Add(new SortColumnDescriptor("Category") is actually becoming GridCaptionSections (which in my book is not exactly logical, there is probably a good explanation for this).

Anyway, I use the following functionality on GroupExpanded and GroupCollapsed (not exactly optimal, but I could not find a better event to use) if anyone else has the same problem (ggcProjects is the name of my GridGroupingControl):

        private void ggcProjects_RecalculateCoveredRanges()
        {
            ggcProjects.TableModel.CoveredRanges.Clear();
 
            int numOfElements = ggcProjects.Table.Elements.Count;
            if (numOfElements > 0)
            {
                int maxNumOfCellMerges = 6;
                int offsetColumns = 2;
                int columnIndexProject = (ggcProjects.TableDescriptor.Columns["Project"].GetRelativeColumnIndex() + offsetColumns);
                int columnIndexProjectNumber = (ggcProjects.TableDescriptor.Columns["Project Number"].GetRelativeColumnIndex() + offsetColumns);
                int columnIndexLink = (ggcProjects.TableDescriptor.Columns["Link"].GetRelativeColumnIndex() + offsetColumns);
 
                for (int i = 0; i < numOfElements; i++)
                {
                    if (ggcProjects.Table.Elements[i] is GridCaptionSection)
                    {
                        GridCaptionSection section = (GridCaptionSection)ggcProjects.Table.Elements[i];
                        if (section.ParentGroup.IsExpanded)
                        {
                            int recordCount = section.ParentGroup.Records.Count;
                            if (recordCount > 0 && recordCount % maxNumOfCellMerges == 0)
                            {
                                int indexTop = section.ParentGroup.Records[0].GetRowIndex();
                                for (int indexMergeTop = indexTop; indexMergeTop < (indexTop + recordCount); indexMergeTop = (indexMergeTop + maxNumOfCellMerges))
                                {
                                    int indexMergeBottom = (indexMergeTop + (maxNumOfCellMerges - 1));
                                    ggcProjects.TableModel.CoveredRanges.Add(GridRangeInfo.Cells(indexMergeTop, columnIndexProject, indexMergeBottom, columnIndexProject));
                                    ggcProjects.TableModel.CoveredRanges.Add(GridRangeInfo.Cells(indexMergeTop, columnIndexProjectNumber, indexMergeBottom, columnIndexProjectNumber));
                                    ggcProjects.TableModel.CoveredRanges.Add(GridRangeInfo.Cells(indexMergeTop, columnIndexLink, indexMergeBottom, columnIndexLink));
                                }
                            }
                        }
                    }
                }
            }
        }


MG Mohanraj Gunasekaran Syncfusion Team February 22, 2017 02:24 PM UTC

Hi Henrick, 

Thanks for your update. 

You have used the right way to achieve your scenario and you can get the column indent count using GetColumnIndentCount method. Also, we have prepared the sample based on your requirement please refer to the below code example and the below sample, 
 
Code example 
this.gridGroupingControl1.Table.GroupExpanded += new GroupEventHandler(Table_GroupExpanded); 
void Table_GroupExpanded(object sender, GroupEventArgs e) 
{ 
    int mergeCellCount = 6; 
    for (int i = 0; i < this.gridGroupingControl1.Table.Elements.Count; i++) 
    { 
        if (this.gridGroupingControl1.Table.Elements[i] is GridRecordRow) 
        { 
            GridRecordRow row = this.gridGroupingControl1.Table.Elements[i] as GridRecordRow; 
            int indentCount = this.gridGroupingControl1.TableDescriptor.GetColumnIndentCount(); 
            int colindex1 = this.gridGroupingControl1.TableModel.NameToColIndex("Project"); 
            int colindex2 = this.gridGroupingControl1.TableModel.NameToColIndex("MergeCol1"); 
            int colindex3 = this.gridGroupingControl1.TableModel.NameToColIndex("MergeCol2"); 
            int colindex4 = this.gridGroupingControl1.TableModel.NameToColIndex("MergeCol3"); 
            for (int j = 0; j < row.ParentGroup.Groups.Count; j++) 
            { 
                int toprowindex = row.ParentGroup.Groups[j].Records[0].GetRowIndex(); 
                int count = row.ParentGroup.Groups[j].Records.Count; 
                int bottomIndex = row.ParentGroup.Groups[j].Records[count - 1].GetRowIndex(); 
                int splitcount = count / mergeCellCount; 
 
                for (int k = 0; k < splitcount; k++) 
                { 
                    int topindex = toprowindex + (k * mergeCellCount); 
                    int bottomindex = topindex - 1 + mergeCellCount; 
                    this.gridGroupingControl1.TableModel.CoveredRanges.Add(GridRangeInfo.Cells(topindex, colindex1, bottomindex, colindex1)); 
                    this.gridGroupingControl1.TableModel.CoveredRanges.Add(GridRangeInfo.Cells(topindex, colindex2, bottomindex, colindex2)); 
                    this.gridGroupingControl1.TableModel.CoveredRanges.Add(GridRangeInfo.Cells(topindex, colindex3, bottomindex, colindex3)); 
                    this.gridGroupingControl1.TableModel.CoveredRanges.Add(GridRangeInfo.Cells(topindex, colindex4, bottomindex, colindex4)); 
                } 
            } 
        } 
    } 
} 
Sample link: GridGroupingControl
Any way we are glad to know that your reported problem has resolved. 
Please let us know if you have any further assistance on this.
Regards,
Mohanraj G
 
  


Loader.
Live Chat Icon For mobile
Up arrow icon