質問

データグリッドビューの背景色をプロパティから「透明」に設定しようとしましたが、「有効なプロパティではなく」と言われました。

どうすればできますか?

役に立ちましたか?

解決

この解決策を特定の問題(グリッドが背景画像付きのフォームに含まれている場合)を簡単に変更した場合、それを適応させて一般的な透明なグリッドを作成できます。グリッドをペイントするために、それがすべてです。

DatagridViewから継承し、次のようなペイントバックグラウンドメソッドをオーバーライドする必要があります。

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;
    }
}

他のヒント

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を保持するだけです。

DatagridView BackgroundColorプロパティに透明な色を持つことは不可能です。

そこで、このプロパティを親のバックカラーと同期することにしました。 Winformsの古き良きデータバインディング機能は、これに非常に優れています:

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

すぐ後 InitializeComponents();

私はこれがかなり古いことを知っていますが、これは非常にうまく機能します。

フォームの色と同じDatagridViewのバックコラーを同じように設定します。これを行うには、DatagridView:[Properties] - > rowtemplate-> defaultCellStyle->バックコラーに移動し、フォームの色を選択します。

すべての行と列を透明に設定する必要があります。より簡単な方法:

for (int y = 0; y < gridName.Rows[x].Cells.Count; y++)
{
     yourGridName.Rows[x].Cells[y].Style.BackColor =
     System.Drawing.Color.Transparent;
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top