ComponentOne FlexGrid for WinForms
Merged Table Headers
Using the C1FlexGrid Control > Merging Cells > Merged Table Headers

To create merged table headers, you must start by setting the grid' s AllowMerging property to FixedOnly. Then, designate the rows and columns that you want to merge by setting the row and column' s AllowMerging properties. Finally, assign the text to the header cells so that the cells you want to merge have the same contents.

The code below shows an example:

To write code in Visual Basic

Visual Basic
Copy Code
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Dim i%
 
    '  Initialize the control.
    _flex.Styles.Normal.WordWrap = True
    _flex.Cols.Count = 9
    _flex.Rows.Fixed = 2
    _flex.AllowMerging = C1.Win.C1FlexGrid.AllowMergingEnum.FixedOnly
 
    '  Create row headers.
    _flex.Rows(0).AllowMerging = True
 
    '  Merge the four cells with same contents.
    Dim rng As C1.Win.C1FlexGrid.CellRange = _flex.GetCellRange(0, 1, 0, 4)
    rng.Data = "North"
 
    '  Merge the four cells with same contents.
    rng = _flex.GetCellRange(0, 5, 0, 8)
    rng.Data = "South"
    For i = 1 To 4
        _flex(1, i) = "Qtr " & i
        _flex(1, i + 4) = "Qtr " & i
    Next
 
    '  Create the column header.
    _flex.Cols(0).AllowMerging = True
 
    '  Merge the two cells with same contents.
    rng = _flex.GetCellRange(0, 0, 1, 0)
    rng.Data = "Sales by Product"
 
    '  Align and autosize the cells.
    _flex.Styles.Fixed.TextAlign = C1.Win.C1FlexGrid.TextAlignEnum.CenterCenter
    _flex.AutoSizeCols(1, _flex.Cols.Count - 1, 10)
End Sub

To write code in C#

C#
Copy Code
private void Form1_Load(System.object sender, System.EventArgs e)
{
    int i;
 
    // Initialize the control.
    _flex.Styles.Normal.WordWrap = true;
    _flex.Cols.Count = 9;
    _flex.Rows.Fixed = 2;
    _flex.AllowMerging = C1.Win.C1FlexGrid.AllowMergingEnum.FixedOnly;
 
    // Create row headers.
    _flex.Rows[0].AllowMerging = true;
 
    // Merge the four cells with same contents.
    C1.Win.C1FlexGrid.CellRange rng = _flex.GetCellRange(0, 1, 0, 4);
    rng.Data = "North";
 
    // Merge the four cells with same contents.
    rng = _flex.GetCellRange(0, 5, 0, 8);
    rng.Data = "South";
    for ( i = 1 ; i <= 4; i++)
    {
        _flex[1, i] = "Qtr " + i;
        _flex[1, i + 4] = "Qtr " + i;
    } 
 
    // Create the column header.
    _flex.Cols[0].AllowMerging = true;
 
    // Merge the two cells with same contents.
    rng = _flex.GetCellRange(0, 0, 1, 0);
    rng.Data = "Sales by Product";
 
    // Align and autosize the cells.
    _flex.Styles.Fixed.TextAlign = C1.Win.C1FlexGrid.TextAlignEnum.CenterCenter;
    _flex.AutoSizeCols(1, _flex.Cols.Count - 1, 10);
}

This is the result:


See Also