Handling Big Red X DataGridView

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();
    }
  }
}

One Reply to “Handling Big Red X DataGridView”

Leave a Reply

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

*