wpf-devexpress-dot-xpf-dot-grid-dot-tableview-cdcfe2ee.md
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
public event CellMergeEventHandler CellMerge
Public Event CellMerge As CellMergeEventHandler
The CellMerge event's data class is CellMergeEventArgs. The following properties provide information specific to this event:
| Property | Description |
|---|---|
| CellValue1 | Gets the value of the first cell processed by the TableView.CellMerge event handler. |
| CellValue2 | Gets the value of the second cell processed by the TableView.CellMerge event handler. |
| Column | Gets the column that contains the cells processed by the TableView.CellMerge event handler. |
| Handled | Gets or sets whether a cell merge operation is handled and no default processing is required. |
| Merge | Gets or sets whether the cells processed by the TableView.CellMerge event handler will be merged. |
| RowHandle1 | Gets the row handle of the first row processed by the TableView.CellMerge event handler. |
| RowHandle2 | Gets the row handle of the second row processed by the TableView.CellMerge event handler. |
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.
The code sample below merges cells in the Order Date column if they have the same month and year:
<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>
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;
}
}
}
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