The reason why sometimes a grid control shows a big red X and stops working properly is because an unhandled exception has occurred during the control’s OnPaint event.

One way to handle this is by extending from the grid control and overriding the OnPaint event so that any exceptions can be handled. Of course, the best solution is to try and prevent such exceptions from happening in the first place.

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

2 Replies to “Handling Big Red X DataGridView”

Leave a Reply

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

The reCAPTCHA verification period has expired. Please reload the page.