True DBGrid for WinForms | ComponentOne
In This Topic
    Merge
    In This Topic

    If the underlying grid data is sorted, the readability of the display may be improved by grouping adjacent like-valued cells within the sorted column(s). The Merge property of the C1DisplayColumn object controls whether its data cells are grouped in this manner to form a single non-editable cell, using the ColumnMergeEnum. By default, this property is set to None, and each physical row within a column displays a data value, if any.

    Consider the following grid, which is sorted by the Country field.

    If data-sensitive cell merging is enabled for the Country column at run time, then its cells are grouped according to their contents. For example:

    C#
    Copy Code
    this.c1TrueDBGrid1.Splits[0].DisplayColumns["Country"].Merge = C1.Win.C1TrueDBGrid.ColumnMergeEnum.Free;                  
    

    Executing this statement produces the following display. Note that when the current cell is in the Country column, the marquee spans all like-valued rows and takes on the appearance of a dotted rectangle, regardless of the setting of the MarqueeStyle property. The behavior of the marquee in other columns is not affected, however.

    If a design-time layout is specified, the same effect can be achieved by setting the Merge property of the desired C1DisplayColumn object within the C1DisplayColumn Collection Editor, which can be accessed by clicking on the ellipsis button (...) after the DisplayColumns property in the Split Collection Editor.

    The Merge property can be set to Free, which combines like values in adjacent rows, or Restricted, which combines like values in adjacent rows in the same row span as the previous column. The difference between Free and Restricted settings is whether cells within the same contents should always be merged (Free) or only when adjacent cells to the left or top are also merged (Restricted). The examples below illustrate the difference.

    No Merge (Regular Spreadsheet View)

    No merge displays data in a regular spreadsheet view.

    C#
    Copy Code
    this.c1TrueDBGrid1.Splits[0].DisplayColumns[0].Merge = C1.Win.C1TrueDBGrid.ColumnMergeEnum.None;
    this.c1TrueDBGrid1.Splits[0].DisplayColumns[1].Merge = C1.Win.C1TrueDBGrid.ColumnMergeEnum.None;
    this.c1TrueDBGrid1.Splits[0].DisplayColumns[2].Merge = C1.Win.C1TrueDBGrid.ColumnMergeEnum.None;
    this.c1TrueDBGrid1.Splits[0].DisplayColumns[3].Merge = C1.Win.C1TrueDBGrid.ColumnMergeEnum.None                   
    

    Free Merge

    Free merge combines like values in adjacent rows.

    Notice how the first Region cell (East) merges across employees (Donna and John) to its left.

    C#
    Copy Code
    // Set free merging.
    this.c1TrueDBGrid1.Splits[0].DisplayColumns[0].Merge = C1.Win.C1TrueDBGrid.ColumnMergeEnum.Free;
    this.c1TrueDBGrid1.Splits[0].DisplayColumns[1].Merge = C1.Win.C1TrueDBGrid.ColumnMergeEnum.Free;
    this.c1TrueDBGrid1.Splits[0].DisplayColumns[2].Merge = C1.Win.C1TrueDBGrid.ColumnMergeEnum.Free;
    this.c1TrueDBGrid1.Splits[0].DisplayColumns[3].Merge = C1.Win.C1TrueDBGrid.ColumnMergeEnum.Free;
     
    // Set each column's vertical alignment to Center.
    this.c1TrueDBGrid1.Splits[0].DisplayColumns[0].Style.VerticalAlignment = C1.Win.C1TrueDBGrid.AlignVertEnum.Center;
    this.c1TrueDBGrid1.Splits[0].DisplayColumns[1].Style.VerticalAlignment = C1.Win.C1TrueDBGrid.AlignVertEnum.Center;
    this.c1TrueDBGrid1.Splits[0].DisplayColumns[2].Style.VerticalAlignment = C1.Win.C1TrueDBGrid.AlignVertEnum.Center;
    this.c1TrueDBGrid1.Splits[0].DisplayColumns[3].Style.VerticalAlignment = C1.Win.C1TrueDBGrid.AlignVertEnum.Center;
    

    Restricted Merge

    Restricted merge combines like values in adjacent rows in the same row span as the previous column.

    Notice how the first Region cell (East) no longer merges across employees to its left.

    C#
    Copy Code
    // Set restricted merging.
    this.c1TrueDBGrid1.Splits[0].DisplayColumns[0].Merge = C1.Win.C1TrueDBGrid.ColumnMergeEnum.Restricted;
    this.c1TrueDBGrid1.Splits[0].DisplayColumns[1].Merge = C1.Win.C1TrueDBGrid.ColumnMergeEnum.Restricted;
    this.c1TrueDBGrid1.Splits[0].DisplayColumns[2].Merge = C1.Win.C1TrueDBGrid.ColumnMergeEnum.Restricted;
    this.c1TrueDBGrid1.Splits[0].DisplayColumns[3].Merge = C1.Win.C1TrueDBGrid.ColumnMergeEnum.None;
     
    // Set each column's vertical alignment to Center.
    this.c1TrueDBGrid1.Splits[0].DisplayColumns[0].Style.VerticalAlignment = C1.Win.C1TrueDBGrid.AlignVertEnum.Center;
    this.c1TrueDBGrid1.Splits[0].DisplayColumns[1].Style.VerticalAlignment = C1.Win.C1TrueDBGrid.AlignVertEnum.Center;
    this.c1TrueDBGrid1.Splits[0].DisplayColumns[2].Style.VerticalAlignment = C1.Win.C1TrueDBGrid.AlignVertEnum.Center;
    this.c1TrueDBGrid1.Splits[0].DisplayColumns[3].Style.VerticalAlignment = C1.Win.C1TrueDBGrid.AlignVertEnum.Center;
    

    If the Merge property is set to Free or Restricted for a column, then none of the data cells can be edited, even if all rows contain unique values. The only exception to this is the AddNew row. However, once a new row is added to the underlying database, then its data will also be uneditable within the merged column(s).

    Note: Merged cells are not limited to displaying text. Display bitmaps within merged cells by populating the ValueItems object as described earlier in Specifying Text-to-Picture Translations. The section Applying Pictures to Grid Elements describes a more flexible method for displaying in-cell graphics using Style objects.

    Formatting Merged Cells

    Use the HorizontalAlignment and VerticalAlignment properties of the column's Style object to center the data within the merged cell, as in the following figure.

    In the Splits Collection Editor, access these properties by expanding the Style property node at the same level of the tree as the Merge property. Or, in code:

    C#
    Copy Code
    C1.Win.C1TrueDBGrid.Style s;
    s = this.c1TrueDBGrid1.Splits[0].DisplayColumns["Country"].Style;
    s.HorizontalAlignment = C1.Win.C1TrueDBGrid.AlignHorzEnum.Center;
    s.VerticalAlignment = C1.Win.C1TrueDBGrid.AlignVertEnum.Center;