How to place a UserControl in the header cell of WinForms GridGroupingControl?
Place a usercontrol in the header cell
In order to have a User Control in WinForms GridGroupingControl, the GridGenericCellModel and GridGenericCellRenderer classes have to be derived and the User Control that is designed must be made as a cellmodel in GridGroupingControl using these derived classes.
The following are the steps that need to be followed:
Step 1: The UserControl form is added to the Project and the required controls are dropped into it. In the UserControl, the OnLayout method is overridden for positioning the inner button controls.
C#
protected override void OnLayout(LayoutEventArgs levent)
{
Hashtable ht = new Hashtable();
foreach (Control c in Controls)
{
if (c is Button)
{
ht[c] = c.Location;
c.Location = new Point(-1000, -1000);
}
}
if (levent != null)
//call the default layout method.
base.OnLayout(levent);
foreach (Control c in Controls)
{
//set the position of the control.
if (c is Button)
c.Location = (Point)ht[c];
}
}
public void ForceLayout()
{
//call the layout method to set the button locations.
OnLayout(null);
}Protected Overrides Sub OnLayout(ByVal levent As LayoutEventArgs)
Dim ht As New Hashtable()
For Each c As Control In Controls
If TypeOf c Is Button Then
ht(c) = c.Location
c.Location = New Point(-1000, -1000)
End If
Next c
If levent IsNot Nothing Then
'call the default layout method.
MyBase.OnLayout(levent)
End If
For Each c As Control In Controls
'set the position of the control.
If TypeOf c Is Button Then
c.Location = CType(ht(c), Point)
End If
Next c
End Sub
Public Sub ForceLayout()
'call the layout method to override the properties.
OnLayout(Nothing)
End SubStep 2: Create CustomControlCellModel by deriving it from GridGenericCellModel class.
C#
public class CustomControlCellModel : GridGenericControlCellModel
{
#region Local Variables
private static CustomControlCellRenderer customRenderer;
private static CustomControl customControl;
#endregion
#region Constructor
public CustomControlCellModel(GridModel grid)
: base(grid)
{
}
#endregion
#region Overridden Methods
//creating Renderer
public override GridCellRendererBase CreateRenderer(GridControlBase control)
{
customRenderer = new CustomControlCellRenderer(control, this);
return customRenderer;
}
#endregion
#region Getters & Setters
//creating the static variable for the control
public static CustomControl CustomControl
{
get
{
return customControl;
}
set
{
customControl = value;
}
}
#endregion
}VB
Public Class CustomControlCellModel
Inherits GridGenericControlCellModel
#Region "Local Variables"
Private Shared customRenderer As CustomControlCellRenderer
Private Shared customControl_Renamed As CustomControl
#End Region
#Region "Constructor"
Public Sub New(ByVal grid As GridModel)
MyBase.New(grid)
End Sub
#End Region
#Region "Overridden Methods"
'creating Renderer.
Public Overrides Function CreateRenderer(ByVal control As GridControlBase) As GridCellRendererBase
customRenderer = New CustomControlCellRenderer(control, Me)
Return customRenderer
End Function
#End Region
#Region "Getters & Setters"
'creating the static variable for the control.
Public Shared Property CustomControl() As CustomControl
Get
Return customControl_Renamed
End Get
Set(ByVal value As CustomControl)
customControl_Renamed = value
End Set
End Property
#End Region
End ClassStep 3: Create CustomControlCellRenderer from the GridGenericCellRenderer class and the UserControl is drawn accordingly to fit into the required cell header.
C#
//Creating CustomControlCellRenderer class.
public class CustomControlCellRenderer: GridGenericControlCellRenderer
{
#region Constructor
public CustomControlCellRenderer(GridControlBase grid, GridCellModelBase cellModel)
: base(grid, cellModel)
{
SupportsFocusControl = true;
}
#endregion
Control host = null;
#region Overridden Methods
protected override void OnDraw(Graphics g, Rectangle clientRectangle, int rowIndex, int colIndex, GridStyleInfo style)
{
if (host == null)
{
host = new Control();
//add the control to the grid.
Grid.Controls.Add(host);
}
CustomControl c = CustomControlCellModel.CustomControl;
//set the control to the parentcontrol.
FixControlParent(c);
//set the specified size to the control.
c.Bounds = clientRectangle;
//assign the control position.
c.ForceLayout();
c.Refresh();
}
#endregion
}VB
Public Class CustomControlCellRenderer
Inherits GridGenericControlCellRenderer
#Region "Constructor"
Public Sub New(ByVal grid As GridControlBase, ByVal cellModel As GridCellModelBase)
MyBase.New(grid, cellModel)
SupportsFocusControl = True
End Sub
#End Region
Private host As Control = Nothing
#Region "Overridden Methods"
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 Is Nothing Then
host = New Control()
'add the control to the grid.
Grid.Controls.Add(host)
End If
Dim c As CustomControl = CustomControlCellModel.CustomControl
'set the control to the parentcontrol.
FixControlParent(c)
'set the specified size to the control.
c.Bounds = clientRectangle
'assign the control position.
c.ForceLayout()
c.Refresh()
End Sub
#End Region
End ClassStep 4: You can create a new CellType by adding the instance of the derived CustomControlCellModel class into the gridGroupingControl’s CellModels.
C#
CustomControlCellModel.CustomControl = new CustomControl();
//adding the user control to the table model.
this.gridGroupingControl1.TableModel.CellModels.Add("CustomControl", new CustomControlCellModel(this.gridGroupingControl1.TableModel));VB
CustomControlCellModel.CustomControl = New CustomControl()
'adding the usercontrol to the tablemodel.
Me.gridGroupingControl1.TableModel.CellModels.Add("CustomControl", New CustomControlCellModel(Me.gridGroupingControl1.TableModel))Step 5: Set the CustomControl into the specific header cell.
C#
private void gridGroupingControl1_QueryCellStyleInfo(object sender, Syncfusion.Windows.Forms.Grid.Grouping.GridTableCellStyleInfoEventArgs e)
{
//check the celltype is a headercell or not.
if(e.TableCellIdentity.TableCellType == GridTableCellType.ColumnHeaderCell && e.TableCellIdentity.Column.Name == "Col3")
{
//assign the celltype as CustomControl.
e.Style.CellType = "CustomControl";
}
e.Handled = true;
}VB
Private Sub gridGroupingControl1_QueryCellStyleInfo(ByVal sender As Object, ByVal e As Syncfusion.Windows.Forms.Grid.Grouping.GridTableCellStyleInfoEventArgs)
'check the celltype is a headercell or not.
If e.TableCellIdentity.TableCellType = GridTableCellType.ColumnHeaderCell AndAlso e.TableCellIdentity.Column.Name = "Col3" Then
'assign the celltype as CustomControl.
e.Style.CellType = "CustomControl"
End If
e.Handled = True
End SubAfter applying the properties, the Grid is displayed as follows.

Figure 1: Grid with the properties
Samples:
C#: UserControl
VB: UserControl