문제

나는 설정하려고 배경 색상의 데이터 표기하"transparent"속성에서만 그것은 말했다"유효하지 않은 객실".

어떻게 할 수 있습니다?

도움이 되었습니까?

해결책

Simples 수정으로 특정 문제 (그리드가 배경 이미지가 포함 된 양식에 포함 된 경우)에 대한 솔루션을 수행했습니다. 일반적인 투명 그리드를 만들기 위해 적응할 수 있습니다. 부모가 배경 이미지를 가지고 있는지 물어보십시오. 그렇지 않으면 부모의 백콜러를 사용하십시오. 그리드를 페인트하는 것이 전부입니다.

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.그것이 실패하려는 경우에는 그 DGV 하는 동안 당신은 아무 ActiveForm 지만,그렇지 않습니다.그것은 또한 가능하면 그냥 지키면 온라인할 수 있습니다 아직도 사용하고 싶은 디자이너,그리고 그것을 삭제한다.

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