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

我该怎么做?

有帮助吗?

解决方案

我对特定问题进行了此解决方案(当网格以带有背景图像的形式包含在形式中时),然后使用简单修改,您可以对其进行调整以创建通用的透明网格,只需询问父级是否具有背景图像要画网格,仅此而已。

您必须从datagridview继承并覆盖这样的油漆式方法:

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

其他提示

我使用Deumber的解决方案来做到这一点,但它可以造成一些麻烦,这些麻烦通过添加微小的改进来避免:

答:滚动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-LINE,并将其删除以供发布。

在datagridview背景彩色属性中具有透明的颜色是不可能的。

因此,我决定将此属性与父母的背色同步。 Winforms的旧数据指标功能非常出色:

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

刚过 InitializeComponents();

我知道这很旧,但是这很好。

将DataGridView的背色与表单的颜色相同。为此,选择DataGridView:转到属性 - > RowTemplate-> DefaultCellstyle-> BackColor,然后选择表单的颜色。

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

for (int y = 0; y < gridName.Rows[x].Cells.Count; y++)
{
     yourGridName.Rows[x].Cells[y].Style.BackColor =
     System.Drawing.Color.Transparent;
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top