Click here to Skip to main content
15,909,939 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have DataGridView in WinForms Application on my Form. Form has a background image.
I want to make my grid transparent too. But its not working by simply changing the BackgroundColor property.

What I have tried:

I tried below code, but gives me the error that "no suitable method found to override"
C#
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;
            }
        }

Can somebody help?

Thanks.
Posted
Updated 30-Sep-16 1:08am
Comments
Maciej Los 30-Sep-16 6:55am    
Have you tried to debug your programme to find out where the error occurs?
Member 12586674 30-Sep-16 6:57am    
No.Its a compile time error.I'm not able to debug.

1 solution

Quote:
no suitable method found to override
Means exactly what it says: there is no method of that name in the base control that you can override.

There is a DataGridView.PaintBackground method: DataGridView.PaintBackground Method (Graphics, Rectangle, Rectangle) (System.Windows.Forms)[^] but unless you derive your control from DataGridView you cannot override it - you cannot override an embedded control method in your form code, only in code for a control which derives from DataGridView.
 
Share this answer
 
Comments
Member 12586674 30-Sep-16 7:14am    
Yes my grid is derived from
System.Windows.Forms.DataGridView

And I can see "Paint" method but not "PaintBackground"
OriginalGriff 30-Sep-16 7:31am    
How did you derive it? What is the class header definition?
Member 12586674 30-Sep-16 9:02am    
I didn't get you. What do you mean by header information?
OriginalGriff 30-Sep-16 9:51am    
public class myClass : ...
Member 12586674 3-Oct-16 6:39am    
DataGridView class is
public class DataGridView : Control, ISupportInitialize
{}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900