views:

1018

answers:

2

i tried to set the background color of a data grid view to be "transparent" from prperties but it it said "not a valid property". How to do it..

A: 

You need to set all the rows and columns to transparent. Easier way is:

for (int y = 0; y < gridName.Rows[x].Cells.Count; y++)
{
     yourGridName.Rows[x].Cells[y].Style.BackColor =
     System.Drawing.Color.Transparent;
}
choudeshell
And you need to nest that for loop in another to get the columns...
choudeshell
and what about th background color of the data rid view itself..
Junaid Saeed
@choudeshell: This should be possible during Design time.
galford13x
+1  A: 

I create 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.

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