我试图将数据 GridView 的背景颜色设置为对属性“透明”,但它显示“不是有效属性”。
我该怎么做?
最佳答案
我通过简单的修改针对特定问题(当网格包含在带有背景图像的表单中时)做了这个解决方案,您可以调整它以创建通用透明网格,只需询问父级是否有背景图像,否则只需使用用于绘制网格的父背景色,仅此而已。
您必须像这样继承自 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;
}
}
关于c# - 将数据 GridView 背景设置为透明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1330220/