Articles in this section
Category / Section

How to create the transparent background for control cell type?

2 mins read

By default, the assigned control of a cell will be drawn as bitmap when the cell type is set to Control. So, the transparent background will be drawn with black background. To create the transparent background, create a CustomCellRenderer derived from the GridGenericControlCellRenderer and override the OnDraw() method. To add the controls with transparent background to the grid, set the location and size of the control based on the obtained cell rectangle using the RangeInfoToRectangle() method.

Code Snippet

Creating CustomCellRenderer

C#

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

 

VB

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

C#

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

 

VB

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)

 

Screenshot

Showing transparent background of GridControl

Sample Links:

C#: Transparent background for control_CS

VB: Transparent background for control_VB

 

 

 

 

 

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied