Set datagrid view background to transparent
Asked Answered
M

5

6

I tried to set the background color of a data grid view to be "transparent" from properties but it it said "not a valid property".

How can I do it?

Mouton answered 25/8, 2009 at 18:47 Comment(1)
Is this error from the Designer?Hebraize
B
9

I did this solution to a specific problem (when the grid was contained in a form with background image) with simples modifications you can adapt it to create a generic transparent Grid, just ask if the parent have background image, else just use the parent backcolor to paint your grid, and that is all.

You must inherit from DataGridView and override the PaintBackground method like this:

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;
    }
}
Bradytelic answered 17/3, 2010 at 13:8 Comment(1)
Under SetCellsTransparent, just use this.DefaultCellStyle to set .BackColor and (optionally - but I wouldn't) .SelectionBackColor. No need for the foreach loop.Dactylology
L
2

i did this with Deumber's solution and it works, but causes some troubles that i avoided by adding small improvements:

A. scrolling the DGV messes up the background. solution: put this somewhere:

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

the background will still scroll, but be corrected immediately after each scroll step. it's noticeable, but was acceptable for me. does anyone know a better solution to support scrolling with this?

B. the designer has troubles using it. solution:

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

now the designer treats it just like a DGV. it will fail if you ever want to draw the DGV while you have no ActiveForm, but that's not the case usually. it's also possible to just keep the if-line while you might still want to use the designer, and delete it for the release.

Lea answered 24/11, 2015 at 20:19 Comment(2)
My only change would be to remove the -3, 3 offset introduced in your Rectangle rectDest. For me, it made the cropped section of the Image noticeably out of alignment.Dactylology
the -3, 3 is probably due to some specifics in my application. i can't remember why, but for some reason i needed it to have the image where i wanted it. if it's missplaced for you, play with those numbers =)Lea
Q
1

Having a transparent color in the DataGridView BackGroundColor property is not possible.

So I decided to sync this property with the parent's BackColor. The good old databinding feature of WinForms is very good at this :

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

Just after InitializeComponents();

I know this is pretty old, but this works very well.

Quintanilla answered 3/4, 2017 at 13:23 Comment(1)
It didn't work in my caseHypotrachelium
M
-1

You need to set all the rows and columns to transparent. Easier way is:

for (int y = 0; y < gridName.Rows[x].Cells.Count; y++)
{
     yourGridName.Rows[x].Cells[y].Style.BackColor =
     System.Drawing.Color.Transparent;
}
Moshe answered 25/8, 2009 at 18:58 Comment(1)
This should be possible during Design time.Hebraize
S
-1

Set datagridview's backcolor same with the form's color. To do this, select datagridview: go to Properties -> RowTemplate -> DefaultCellStyle -> BackColor and choose the color of your form.

Sprightly answered 7/5, 2015 at 9:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.