2009-08-25 129 views
3

データグリッドビューの背景色をプロパティから「透明」に設定しようとしましたが、「有効なプロパティではありません」と言いました。DataGridビューの背景を透明に設定する

どうすればいいですか?

+0

このエラーはデザイナーからのものですか? – galford13x

答えて

6

(グリッドは、背景画像と形に含まれていた場合)私は最も簡単な修正を加えて特定の問題にこのソリューションをしたあなたは、一般的な透明グリッドを作成するためにそれを適応させることができ、場合だけ頼みます親は背景画像を持っています。そうでなければ、親の背景色を使用してグリッドを塗りつぶすだけです。

あなたは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'の下で、' .BackColor'と '' .SelectionBackColor'を設定するために 'this.DefaultCellStyle'を使います。 'foreach'ループの必要はありません。 – OhBeWise

-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

+0

とデータのビューの背景色はどうですか? – Moon

+0

これはデザイン時に可能です。 – galford13x

-1

セットのDataGridViewの背景色を。これを行うには、datagridviewを選択します。プロパティ - > RowTemplate - > DefaultCellStyle - > BackColorに移動し、フォームの色を選択します。

2

私はDeumberのソリューションでこれを行なったし、それが動作しますが、私は小さな改善を加えることによって回避するいくつかのトラブルが発生します。DGVをスクロール

A.バックグラウンドを台無しに。解決策:これをどこかに置きます:

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'オフセットを削除することだけです。私にとっては、Imageの切り抜かれた部分が目立たないようにしました。 – OhBeWise

+0

'-3、3'はおそらく私のアプリケーションのいくつかの特質のためです。私は理由を覚えていないが、何らかの理由で私はそれが私が望むところにイメージを持っている必要があった。それがあなたのために配置されていない場合、それらの番号で遊ぶ=) – letsdance

0

DataGridViewのBackGroundColorプロパティで透明な色を使用することはできません。

私はこのプロパティを親のBackColorと同期させることに決めました。リサイズの古き良きデータバインディング機能は、この時に非常に良いです:

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

だけ

InitializeComponents();た後、私はこれはかなり古いですけど、これは非常によく動作します。

関連する問題