How to create the transparent background for control cell type in WinForms GridControl?
By default, the assigned control of a cell will be drawn as a bitmap when the cell type is set to our WinForms GridControl page, so the transparent background will be drawn with a black background. To create the transparent background, create a CustomCellRenderer derived from the GridGenericControlCellRenderer and override the OnDraw method. To add the controls with a transparent background to the grid, set the location and size of the control based on the obtained cell rectangle using the RangeInfoToRectangle method.
Creating CustomCellRenderer
public class CustomGenericControlCellRenderer : GridGenericControlCellRenderer
{
Control rControl = null;
Rectangle bounds = Rectangle.Empty;
public CustomGenericControlCellRenderer(GridControlBase grid, GridCellModelBase cellModel) : base(grid, cellModel)
{
}
protected override void OnDraw(Graphics g, Rectangle clientRectangle, int rowIndex, int colIndex, GridStyleInfo style)
{
Control control = style.Control;
if (control != null)
{
if (this.ShouldDrawFocused(rowIndex, colIndex))
{
// Position current grid.
control.Size = clientRectangle.Size;
control.Location = clientRectangle.Location;
if (!control.ContainsFocus)
{
control.Focus();
}
}
else
{
//Render control to bitmap and then draw the bitmap.
Bitmap bmp = null;
Size size = EnlargeWithScrollbars(clientRectangle.Size);
if (control is AxHost)
{
AxHost axHost = (AxHost)control;
object ocx = axHost.GetOcx();
BeginResizeNoPaint(axHost, size);
FixControlParent(control);
bmp = ActiveXSnapshot.TakeSnapshot(ocx);
EndResizeNoPaint();
}
}
}
}
}
Public Class CustomGenericControlCellRenderer
Inherits GridGenericControlCellRenderer
Private rControl As Control = Nothing
Private bounds As Rectangle = Rectangle.Empty
Public Sub New(ByVal grid As GridControlBase, ByVal cellModel As GridCellModelBase)
MyBase.New(grid, cellModel)
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)
Dim control As Control = style.Control
If control IsNot Nothing Then
If Me.ShouldDrawFocused(rowIndex, colIndex) Then
'Position current grid.
control.Size = clientRectangle.Size
control.Location = clientRectangle.Location
If Not control.ContainsFocus Then
control.Focus()
End If
Else
'Render control to bitmap and then draw the bitmap.
Dim bmp As Bitmap = Nothing
Dim size As Size = EnlargeWithScrollbars(clientRectangle.Size)
If TypeOf control Is AxHost Then
Dim axHost As AxHost = CType(control, AxHost)
Dim ocx As Object = axHost.GetOcx()
BeginResizeNoPaint(axHost, size)
FixControlParent(control)
bmp = ActiveXSnapshot.TakeSnapshot(ocx)
EndResizeNoPaint()
End If
End If
End If
End Sub
End Class
Setting the control to the grid
this.gridControl1.CellRenderers["Control"] = new CustomGenericControlCellRenderer(this.gridControl1, new GridGenericControlCellModel(this.gridControl1.Model));
this.gridControl1.SuspendLayout();
for (int i = 1; i <= this.gridControl1.RowCount; i++)
{
this.gridControl1[i, 1].CellType = "Control";
UserControl2 uc = new UserControl2();
uc.BackColor = Color.Transparent;
this.gridControl1[i, 1].Control = uc;
Rectangle rect = this.gridControl1.RangeInfoToRectangle(GridRangeInfo.Cell(i, 1));
//Setting the size and location of usercontrol.
uc.Location = rect.Location;
uc.Size = new Size(this.gridControl1.ColWidths[1] - 1, this.gridControl1.DefaultRowHeight - 1);
this.gridControl1.Controls.Add(uc);
}
this.gridControl1.ResumeLayout(true);
Me.gridControl1.CellRenderers("Control") = New CustomGenericControlCellRenderer(Me.gridControl1, New GridGenericControlCellModel(Me.gridControl1.Model))
Me.gridControl1.SuspendLayout()
For i As Integer = 1 To Me.gridControl1.RowCount
Me.gridControl1(i, 1).CellType = "Control"
Dim uc As New UserControl2()
uc.BackColor = Color.Transparent
Me.gridControl1(i, 1).Control = uc
Dim rect As Rectangle = Me.gridControl1.RangeInfoToRectangle(GridRangeInfo.Cell(i, 1))
’Setting the size and location of usercontrol.
uc.Location = rect.Location
uc.Size = New Size(Me.gridControl1.ColWidths(1) - 1, Me.gridControl1.DefaultRowHeight - 1)
Me.gridControl1.Controls.Add(uc)
Next i
Me.gridControl1.ResumeLayout(True)
The Screenshot below illustrates the GridControl with a transparent background.
Conclusion
I hope you enjoyed learning how to create the transparent background for control cell type in WinForms GridControl.
You can refer to our WinForms GridControl feature tour page to know about its other groundbreaking feature representations and WinForms GridControl documentation, and how to quickly get started for configuration specifications.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!