[]
        
(Showing Draft Content)

Custom Merge

FlexGrid, by default, gives you enough options to merge in most common scenarios using AllowMerging 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 C1FlexGrid 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


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);
   }               
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.

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