How to Place UserControl in a Header Cell

3 Jan 20203 minutes to read

To have User Control in GridGroupingControl, GridGenericCellModel and GridGenericCellRenderer classes must be derived and User Control designed must be made as a cell model in the GridGroupingControl using these derived classes.

The following are the steps that need to be followed to do this.

Step 1: The UserControl form is added to the project and the required controls are dropped into it.

Step 2: The GridGenericCellModel and GridGenericCellRenderer classes are derived and the UserControl is drawn accordingly to fit into the required cell header.

public class CustomControlCellRenderer: GridGenericControlCellRenderer
    Control host = null;
    public CustomControlCellRenderer(GridControlBase grid, GridCellModelBase cellModel)
    : base(grid, cellModel)
        SupportsFocusControl = true;
   protected override void OnDraw(Graphics g, Rectangle clientRectangle, int rowIndex, int colIndex, GridStyleInfo style)
       if (host == null)
           host = new Control();
       Button c = CustomControlCellModel.CustomControl;
       c.Bounds = clientRectangle;

Public Class CustomControlCellRenderer Inherits GridGenericControlCellRenderer
Private host As Control = Nothing
Public Sub New(ByVal grid As GridControlBase, ByVal cellModel As GridCellModelBase)
MyBase.New(grid, cellModel)
SupportsFocusControl = true
End Sub
Protected Overrides Sub OnDraw(ByVal g As Graphics, ByVal clientRectangle As Rectangle, ByVal rowIndex As Integer, ByVal colIndex As Integer, ByVal style As GridStyleInfo)
If (host = Nothing) Then
host = New Control
End If
Dim c As Button = CustomControlCellModel.CustomControl
c.Bounds = clientRectangle
End Sub
End Class

Step 3: A new CellType is created by adding the instance of derived generic cell model class into the GridGroupingControl’s CellModels.

CustomControlCellModel.CustomControl = new Button();
this.gridGroupingControl1.Table.TableModel.CellModels.Add("UserControl",new CustomControlCellModel(this.gridGroupingControl1.TableModel));
CustomControlCellModel.CustomControl = New Button
Me.gridGroupingControl1.Table.TableModel.CellModels.Add("UserControl", New CustomControlCellModel(Me.gridGroupingControl1.TableModel))