Click here to Skip to main content
15,890,282 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi,
I am using a gridview which updates values periodically as well as automatically(based on autorefresh timer)

My Problem is that I get a Big Red Cross over my gridview.
I googled out and searched a code to override OnPaint Event. But It fails to avoid
the Red Cross over my datagridview.

I tried creating a Datagridview instead of mine

C#
public class SafeDataGridView : DataGridView
{
  protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
  {
    try
    {
      base.OnPaint(e);
    }
    catch (Exception)
    {
      this.Invalidate();
    }
  }
}


I have attached a link which shows the sample picture of my problem
http://koenaerts.ca/wp-content/uploads/dgvredx.png

Please help Me to get rid of this problem
Posted
Updated 2-Sep-15 4:09am
v2

1 solution

I guess, this is a diagonal red cross showing just in the design mode, meaning that the rendering cannot be performed at design time. Try to comment out rendering code, and you will see.

Your code sample has one major bug: call to Invalidate from the rendering code. It actually means "infinite" repetition of rendering, infinite recursion. Apparently, you have no clue how it works. Please see my past answers:
What kind of playful method is Paint? (DataGridViewImageCell.Paint(...))[^].

Are you getting the idea? See some more of those answers:
capture the drawing on a panel[^],
Drawing Lines between mdi child forms[^],
How to speed up my vb.net application?[^],
Zoom image in C# .net mouse wheel[^].

Final advice: don't overuse the designer; doing so means a lot of dumb manual work, which is hard to maintain. And protect your code running under the designer from such disasters when a designer mode cannot show at all, which is a common case. You can execute as little of questionable code in design mode as possible, which can be achieved by using this check:
C#
if (!DesignMode)
   // do something which may fail

Please see: https://msdn.microsoft.com/en-us/library/system.componentmodel.component.designmode(v=vs.110).aspx[^].

—SA
 
Share this answer
 
v3
Comments
[no name] 2-Sep-15 11:44am    
Great side notes which helped me a lot to open my mind. My 5.
Sergey Alexandrovich Kryukov 2-Sep-15 11:46am    
Thank you.
—SA

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