将datagrid视图背景设置为透明

Moo*_*oon 5 c# transparency datagridview background-color winforms

我试图将数据网格视图的背景颜色设置为从属性"透明",但它表示"不是有效的属性".

我该怎么做?

RJa*_*nes 7

我对一个特定的问题做了这个解决方案(当网格包含在带有背景图像的表格中)时进行简单的修改,你可以调整它来创建一个通用的透明网格,只要问父母是否有背景图像,否则只需使用父背景颜色绘制你的网格,就是这样.

您必须从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;
    }
}
Run Code Online (Sandbox Code Playgroud)