Set datagrid data view background to transparent

I tried to set the background color of the data grid as “transparent” from the properties, but he said “invalid property”.

How can i do this?

+3
source share
5 answers

I made this solution for a specific problem (when the grid was contained in a form with a background image) using simple modifications that you can adapt to create a common transparent grid, just ask if the parent has a background image, otherwise just use the parent backcolor to draw your grid, and that’s it.

DataGridView PaintBackground :

protected override void PaintBackground(Graphics graphics, Rectangle clipBounds,  Rectangle gridBounds)
  {
    base.PaintBackground(graphics, clipBounds, gridBounds);
    Rectangle rectSource = new Rectangle(this.Location.X, this.Location.Y, this.Width, this.Height);
    Rectangle rectDest = new Rectangle(0, 0, rectSource.Width, rectSource.Height);

    Bitmap b = new Bitmap(Parent.ClientRectangle.Width, Parent.ClientRectangle.Height);
    Graphics.FromImage(b).DrawImage(this.Parent.BackgroundImage, Parent.ClientRectangle);


    graphics.DrawImage(b, rectDest, rectSource, GraphicsUnit.Pixel);
    SetCellsTransparent();
  }


public void SetCellsTransparent()
{
    this.EnableHeadersVisualStyles = false;
    this.ColumnHeadersDefaultCellStyle.BackColor = Color.Transparent;
    this.RowHeadersDefaultCellStyle.BackColor = Color.Transparent;


    foreach (DataGridViewColumn col in this.Columns)
    {
        col.DefaultCellStyle.BackColor = Color.Transparent;
        col.DefaultCellStyle.SelectionBackColor = Color.Transparent;
    }
}
+6

Deumber, , , , :

. DGV . : -:

public partial class main : Form
{ 
    protected override CreateParams CreateParams 
    {
    get
        {
        CreateParams cp = base.CreateParams;
        cp.ExStyle |= 0x02000000;
        return cp;
        }
    }
}

- , . , . - ?

. . :

protected override void PaintBackground(Graphics graphics, Rectangle clipBounds, Rectangle gridBounds)
{
    base.PaintBackground(graphics, clipBounds, gridBounds);
    if (main.ActiveForm != null && this.Parent.BackgroundImage != null)
    {
        Rectangle rectSource = new Rectangle(this.Location.X, this.Location.Y, this.Width, this.Height);
        Rectangle rectDest = new Rectangle(-3, 3, rectSource.Width, rectSource.Height);
        Bitmap b = new Bitmap(Parent.ClientRectangle.Width, Parent.ClientRectangle.Height);
        Graphics.FromImage(b).DrawImage(this.Parent.BackgroundImage, Parent.ClientRectangle);
        graphics.DrawImage(b, rectDest, rectSource, GraphicsUnit.Pixel);
        SetCellsTransparent();
    }
}

DGV. , - DGV, ActiveForm, . if-, , .

+2

DataGridView BackGroundColor .

, BackColor. WinForms :

myDataGridView.DataBindings.Add(nameof(DataGrid.BackgroundColor), 
                                this, 
                                nameof(Control.BackColor));

InitializeComponents();

, , .

0

datagridview , . datagridview: Properties → RowTemplate → DefaultCellStyle → BackColor .

-1

You need to set the transparency of all rows and columns. The easiest way:

for (int y = 0; y < gridName.Rows[x].Cells.Count; y++)
{
     yourGridName.Rows[x].Cells[y].Style.BackColor =
     System.Drawing.Color.Transparent;
}
-2
source

Source: https://habr.com/ru/post/1716027/


All Articles