C# 将数据网格视图背景设置为透明

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1330220/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 15:13:49  来源:igfitidea点击:

Set datagrid view background to transparent

c#winformsdatagridviewtransparencybackground-color

提问by Moon

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?

我该怎么做?

采纳答案by RJardines

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:

您必须从 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;
    }
}

回答by choudeshell

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

回答by tanzer

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.

将 datagridview 的背景色设置为与表单的颜色相同。为此,选择 datagridview:转到 Properties -> RowTemplate -> DefaultCellStyle -> BackColor 并选择表单的颜色。

回答by letsdance

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

我使用 Deumber 的解决方案做到了这一点并且它有效,但会导致一些我通过添加小的改进避免的麻烦:

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

A. 滚动 DGV 会弄乱背景。解决方案:把它放在某个地方:

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:

B. 设计者在使用时遇到麻烦。解决方案:

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.

现在设计师把它当作DGV。如果您想在没有 ActiveForm 的情况下绘制 DGV,它将失败,但通常情况并非如此。也可以只保留 if 行,而您可能仍想使用设计器,并在发布时将其删除。

回答by Larry

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

在 DataGridView BackGroundColor 属性中具有透明颜色是不可能的。

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

所以我决定将此属性与父级的 BackColor 同步。WinForms 的旧数据绑定功能非常擅长:

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

Just after InitializeComponents();

刚过 InitializeComponents();

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

我知道这已经很老了,但这非常有效。