6

我试图将数据网格视图的背景颜色设置为从属性中“透明”,但它说“不是有效属性”。

我该怎么做?

4

5 回答 5

8

我对一个特定问题(当网格包含在带有背景图像的表单中)做了这个解决方案,通过简单的修改你可以调整它来创建一个通用的透明网格,只需询问父级是否有背景图像,否则只需使用父级背景色绘制你的网格,仅此而已。

您必须从 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;
    }
}
于 2010-03-17T13:08:14.653 回答
2

我用 Deumber 的解决方案做到了这一点,它可以工作,但会导致一些麻烦,我通过添加一些小的改进来避免:

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

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

背景仍会滚动,但在每个滚动步骤后会立即更正。这很明显,但对我来说是可以接受的。有谁知道更好的解决方案来支持滚动?

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

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

于 2015-11-24T20:19:57.390 回答
1

不可能在 DataGridView BackGroundColor 属性中使用透明颜色。

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

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

刚过InitializeComponents();

我知道这已经很老了,但这很好用。

于 2017-04-03T13:23:48.453 回答
-1

您需要将所有行和列设置为透明。更简单的方法是:

for (int y = 0; y < gridName.Rows[x].Cells.Count; y++)
{
     yourGridName.Rows[x].Cells[y].Style.BackColor =
     System.Drawing.Color.Transparent;
}
于 2009-08-25T18:58:23.517 回答
-1

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

于 2015-05-07T09:04:27.407 回答