Back to Devexpress

TableView.CellMerge Event

wpf-devexpress-dot-xpf-dot-grid-dot-tableview-cdcfe2ee.md

latest4.8 KB
Original Source

TableView.CellMerge Event

Allows you to specify custom cell merge rules.

Namespace : DevExpress.Xpf.Grid

Assembly : DevExpress.Xpf.Grid.v25.2.dll

NuGet Package : DevExpress.Wpf.Grid.Core

Declaration

csharp
public event CellMergeEventHandler CellMerge
vb
Public Event CellMerge As CellMergeEventHandler

Event Data

The CellMerge event's data class is CellMergeEventArgs. The following properties provide information specific to this event:

PropertyDescription
CellValue1Gets the value of the first cell processed by the TableView.CellMerge event handler.
CellValue2Gets the value of the second cell processed by the TableView.CellMerge event handler.
ColumnGets the column that contains the cells processed by the TableView.CellMerge event handler.
HandledGets or sets whether a cell merge operation is handled and no default processing is required.
MergeGets or sets whether the cells processed by the TableView.CellMerge event handler will be merged.
RowHandle1Gets the row handle of the first row processed by the TableView.CellMerge event handler.
RowHandle2Gets the row handle of the second row processed by the TableView.CellMerge event handler.

Remarks

Set the TableView.AllowCellMerge property to true to merge adjacent cells in each column if they have matching values.

The CellMerge event allows you to control how the GridControl merges cells. For example, you can merge adjacent cells with different values based on custom logic.

If you want to maintain a clean MVVM pattern and specify custom merge rules in a View Model, create a command and bind it to the CellMergeCommand property.

Example

The code sample below merges cells in the Order Date column if they have the same month and year:

xaml
<dxg:GridControl ItemsSource="{Binding Orders}">
    <dxg:GridColumn FieldName="OrderDate" AllowCellMerge="True">
        <dxg:GridColumn.EditSettings>
            <dxe:DateEditSettings DisplayFormat="MMMM yyyy"/>
        </dxg:GridColumn.EditSettings>
    </dxg:GridColumn>
    <!-- ... -->
    <dxg:GridControl.View>
        <dxg:TableView x:Name="view" 
                       CellMerge="view_CellMerge">
    </dxg:TableView>
    </dxg:GridControl.View>
</dxg:GridControl>
csharp
void view_CellMerge(object sender, DevExpress.Xpf.Grid.CellMergeEventArgs e) {
    if (e.Column.FieldName == nameof(Order.OrderDate)) {
        if (((DateTime)e.CellValue1).Month == ((DateTime)e.CellValue2).Month 
        && ((DateTime)e.CellValue1).Year == ((DateTime)e.CellValue2).Year) {
            e.Merge = true;
            e.Handled = true;
        }
    }
}
vb
Private Sub view_CellMerge(ByVal sender As Object, ByVal e As DevExpress.Xpf.Grid.CellMergeEventArgs)
    If e.Column.FieldName = NameOf(Order.OrderDate) Then

        If (CType(e.CellValue1, DateTime)).Month = (CType(e.CellValue2, DateTime)).Month AndAlso (CType(e.CellValue1, DateTime)).Year = (CType(e.CellValue2, DateTime)).Year Then
            e.Merge = True
            e.Handled = True
        End If
    End If
End Sub

See Also

TableView.AllowCellMerge

ColumnBase.AllowCellMerge

TableView Class

TableView Members

DevExpress.Xpf.Grid Namespace