Transparent background for a DataGridView, C#

Posted Wednesday, March 19th 2014 in Programming - Permalink

The DataGridView in C# does not support a transparent background. However, we can simulate one by overriding the background painting method. To do this, create a new class that inherits DataGridView, and override the PaintBackground function. An example follows:

class TransparentDataGridView : DataGridView
        protected override void PaintBackground(Graphics pGraphics, Rectangle pClipBounds, Rectangle pGridBounds)
            Rectangle lSource = new Rectangle(this.Location.X, this.Location.Y, this.Width, this.Height);
            Rectangle lDest = new Rectangle(0, 0, lSource.Width, lSource.Height);

            Bitmap lBitmap = new Bitmap(this.Width, this.Height);

            Graphics.FromImage(lBitmap).DrawImage(this.Parent.BackgroundImage, lDest, lSource, GraphicsUnit.Pixel);

            pGraphics.DrawImage(lBitmap, lDest, lDest, GraphicsUnit.Pixel);

List Posts Newest Posts Page 1Next Page