Back to Devexpress

GridView.CellMerge Event

windowsforms-devexpress-dot-xtragrid-dot-views-dot-grid-dot-gridview-9250a01c.md

latest5.4 KB
Original Source

GridView.CellMerge Event

Allows you to manually merge cells.

Namespace : DevExpress.XtraGrid.Views.Grid

Assembly : DevExpress.XtraGrid.v25.2.dll

NuGet Packages : DevExpress.Win.Grid, DevExpress.Win.Navigation

Declaration

csharp
[DXCategory("Merging")]
public event CellMergeEventHandler CellMerge
vb
<DXCategory("Merging")>
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 being merged.
CellValue2Gets the value of the second cell being merged.
ColumnGets the column that contains the values to be merged.
HandledGets or sets whether the cell merging operation is handled and therefore no default processing is required.
MergeGets or sets whether two cells should be merged.
RowHandle1Gets the handle of the row which contains the first of two cells that are to be merged.
RowHandle2Gets the handle of the row which contains the second of two cells that are to be merged.

Remarks

The CellMerge event is raised if the View’s AllowCellMerge option is enabled.

Handle the CellMerge event to decide whether to merge two adjacent cells. To merge cells, set e.Merge and e.Handled event parameters to true. The e.Handled event parameter indicates that the default processing is not required. If the e.Handled parameter is set to false , the View uses the default merge mechanism (cells are merged if they have matching values).

To prevent cells from being merged, set e.Merge to false and e.Handled to true.

Read the following topics for additional information:

Example

Assume that the “Order Date” column contains date/time values. If the View’s GridOptionsView.AllowCellMerge option is set to true the column’s adjacent cells will be merged if they have matching date/time values (the date and time parts of the values should be equal). The following example shows how to merge the column’s cells which have matching date parts but may have different values for the time parts. To override the default cell merging mechanism the GridView.CellMerge event is handled.

csharp
using DevExpress.XtraGrid.Views.Grid;
// ...
private void gridView1_CellMerge(object sender, CellMergeEventArgs e) {
   if(e.Column.FieldName == "Order Date") {
      GridView view = sender as GridView;
      DateTime val1 = (DateTime)view.GetRowCellValue(e.RowHandle1, e.Column);
      DateTime val2 = (DateTime)view.GetRowCellValue(e.RowHandle2, e.Column);
      e.Merge = val1.Date == val2.Date;
      e.Handled = true;
   }
}
vb
Imports DevExpress.XtraGrid.Views.Grid
' ...
Private Sub GridView1_CellMerge(ByVal sender As Object, _
ByVal e As CellMergeEventArgs) Handles GridView1.CellMerge
   If (e.Column.FieldName = "Order Date") Then
      Dim view As GridView = CType(sender, GridView)
      Dim val1 As DateTime = view.GetRowCellValue(e.RowHandle1, e.Column)
      Dim val2 As DateTime = view.GetRowCellValue(e.RowHandle2, e.Column)
      e.Merge = (val1.Date = val2.Date)
      e.Handled = True
   End If
End Sub

See Also

Tutorial: Cell Merging

GridView Class

GridView Members

DevExpress.XtraGrid.Views.Grid Namespace