2009-08-25 162 views

回答

6

我這樣做解決方案(當電網被包含在與背景圖像的形式)的特定問題simples修改就可以適應它創建一個通用的透明網格,只問如果父母有背景圖像,否則只需使用父背景顏色來繪製網格即可。

必須從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; 
    } 
} 
+0

在'SetCellsTransparent'下,只需使用'this.DefaultCellStyle'來設置'.BackColor'和(可選 - 但我不會)'.SelectionBackColor'。不需要'foreach'循環。 – OhBeWise 2016-12-22 16:23:35

-2

您需要將所有行和列設置爲透明。更簡單的方法是:

for (int y = 0; y < gridName.Rows[x].Cells.Count; y++) 
{ 
    yourGridName.Rows[x].Cells[y].Style.BackColor = 
    System.Drawing.Color.Transparent; 
} 
+0

而你需要將其嵌入到另一個循環中才能獲得列... – choudeshell 2009-08-25 18:58:48

+0

以及數據清除視圖本身的背景顏色.. – Moon 2009-08-26 03:28:31

+0

這應該可以在設計期間進行。 – galford13x 2010-03-18 14:26:14

-1

集的DataGridView的背景色相同與窗體的顏色。爲此,請選擇datagridview:轉到屬性 - > RowTemplate - > DefaultCellStyle - > BackColor,然後選擇表單的顏色。

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-line的同時繼續使用設計器,並刪除該版本。

+0

我唯一的改變就是去除'Rectangle rectDest'中引入的'-3,3'偏移量。對我而言,它使圖像的裁剪部分明顯不符合要求。 – OhBeWise 2016-12-22 16:18:05

+0

'-3,3'可能是由於我的應用程序中的一些細節。我不記得爲什麼,但由於某種原因,我需要它有我想要的圖像。如果它放錯了位置,請使用這些號碼=) – letsdance 2017-03-11 07:50:17

0

在DataGridView BackGroundColor屬性中具有透明顏色是不可能的。

所以我決定將這個屬性與父母的BackColor同步。的WinForms的好老的數據綁定功能是在這個非常好:

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

剛過InitializeComponents();

我知道這是很老了,但是這個作品非常好。