# Custom Merge

FlexGrid for WinForms allows you to implement custom merging in case built-in merging does not meet the requirements. Know how to implement custom merging here.

## Content

FlexGrid, by default, gives you enough options to merge in most common scenarios using <span data-popup-content="This property is available in both \u003ca href=\u0022/componentone/docs/win/online-flexgrid/\u0022\u003e.NET Framework\u003c/a\u003e and \u003ca href=\u0022/componentone/docs/win/online-flexgrid/\u0022\u003e.NET\u003c/a\u003e." data-popup-title="AllowMerging" data-popup-theme="ui-tooltip-green qtip-green">AllowMerging</span> property. However, there may be instances where you want to customize the merging options to get more out of your grid. You can customize the default merging behavior in two ways:

## Method 1: Use IComparer Interface

By default, the grid merges adjacent cells containing same non-null value. In default scenario, string comparisons are case-sensitive and blanks are included. However, you can also merge cells using a case-insensitive comparison and trimming blanks by writing a custom class that implements **IComparer** interface and assign it to **CustomComparer** property of the <span data-popup-content="This class is available in both \u003ca href=\u0022/componentone/docs/win/online-flexgrid/\u0022\u003e.NET Framework\u003c/a\u003e and \u003ca href=\u0022/componentone/docs/win/online-flexgrid/\u0022\u003e.NET\u003c/a\u003e." data-popup-title="C1FlexGrid" data-popup-theme="ui-tooltip-green qtip-green">C1FlexGrid</span> class.

## Method 2: Override the GetMergedRange Method

Another way is to create a new class that derives from the **C1FlexGrid** class and overrides the **GetMergedRange** and **GetData** methods to provide your own custom merging logic. It merges the cells based on contextual understanding of data in FlexGrid, which is implemented through the overridden methods in the sample. The below example demonstrates custom merging in timetable of each lecturer using the WinForms FlexGrid.

![Custom Merging](https://cdn.mescius.io/document-site-files/images/2f10b028-0ae8-4c29-a102-4d67578c339b/images/custom-merge.png)

```csharp
public override CellRange GetMergedRange(int row, int col, bool clip)
{
  // Save index of ID column to use in merging logic
  _colIndex = Cols.IndexOf("LecturerID");
  // Set flag to use custom data when merging
  _doingMerge = true;
  // Call base class merging logic (will retrieve data using GetData method)
  CellRange cellRange = base.GetMergedRange(row, col, clip);
  // Reset flag so GetData behaves as usual
  _doingMerge = false;
  // Return the merged range
  return cellRange;
 }
 public override Object GetData(int row, int col)
  {
    // Getting data to determine merging range:
    // Append content of ID column to avoid merging cells in rows with different IDs
    if (_doingMerge && _colIndex > -1 && col != _colIndex)
         {
          System.Diagnostics.Debug.WriteLine($"{row},{col}");
          return base.GetDataDisplay(row, col) + base.GetDataDisplay(row, _colIndex);
         }
   // Getting data to display, measure, edit etc.
   // Let base class handle it as usual
   return base.GetData(row, col);
   }               
```

```vbnet
Public Overrides Function GetMergedRange(ByVal row As Integer, ByVal col As Integer, ByVal clip As Boolean) As CellRange
    ' Save index of ID column to use in merging logic
    _colIndex = Cols.IndexOf("LecturerID")
    ' Set flag to use custom data when merging
    _doingMerge = True
    ' Call base class merging logic (will retrieve data using GetData method)
    Dim cellRange As CellRange = MyBase.GetMergedRange(row, col, clip)
    ' Reset flag so GetData behaves as usual
    _doingMerge = False
    ' Return the merged range
    Return cellRange
End Function
Public Overrides Function GetData(ByVal row As Integer, ByVal col As Integer) As Object
    ' Getting data to determine merging range:
    ' Append content of ID column to avoid merging cells in rows with different IDs
    If _doingMerge AndAlso _colIndex > -1 AndAlso col <> _colIndex Then
        Debug.WriteLine($"{row},{col}")
        Return MyBase.GetDataDisplay(row, col) + MyBase.GetDataDisplay(row, _colIndex)
    End If
    ' Getting data to display, measure, edit etc.
    ' Let base class handle it as usual
    Return MyBase.GetData(row, col)
End Function   
```

For detailed implementation of more scenarios of custom merging, see the product Projects named **CustomMerge**, **CustomMerge2**, **CustomMerge3**, **CustomMerge4**.

>type=note
> **Note**: The abovementioned product samples are located at \\Documents\\ComponentOne Samples\\WinForms\\vx.x.x\\C1FlexGrid\\CS on your system, if you have installed the samples while installing WinForms Edition using **ComponentOneControlPanel.exe**.

## See Also

**Blog**

[Custom Cell Merging in WinForms FlexGrid](https://www.grapecity.com/blogs/custom-cell-merging-in-winforms-flexgrid)