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?
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?
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;
}
}
SetCellsTransparent
, just use this.DefaultCellStyle
to set .BackColor
and (optionally - but I wouldn't) .SelectionBackColor
. No need for the foreach
loop. –
Dactylology 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.
-3, 3
offset introduced in your Rectangle rectDest
. For me, it made the cropped section of the Image noticeably out of alignment. –
Dactylology -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 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.
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;
}
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.
© 2022 - 2024 — McMap. All rights reserved.