将datagrid视图背景设置为透明 [英] Set datagrid view background to transparent
问题描述
我试图将数据网格视图的背景颜色设置为属性透明,但它表示不是有效的属性。
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".
如何我做了吗?
推荐答案
我做了这个解决方案的一个具体的问题(当网格包含在一个表单与背景图像)通过简单的修改,您可以调整它来创建一个通用的透明网格,只要问父母是否有背景图像,否则只需使用父背景来绘制网格,这就是所有。
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.
您必须继承DataGridView并覆盖PaintBackground方法,如下所示:
You must inherit from DataGridView and override the PaintBackground method like this:
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;
}
}
这篇关于将datagrid视图背景设置为透明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!