DataGridView Red "X"
-
Thursday, January 25, 2007 4:12 PM
Hey all,
I was wondering if anyone has seen an annoying Red Cross on their DataGridView controls yet in .NET 2.0; and if you have any pointers on what causes it or how to resolve them? So far it only occurs on the Q&A team’s systems that they are testing the system on; yet I can’t replicate this on my machine (or any of us developers for that matter). The only resolution I’ve been able to try and isolate and ratify it, is to set the data source to nothing and then rebind it on refresh… It is still odd that this happens though and I can’t seem to find much help on the net or google (before anyone says JFGI J)
I know it is because the object is not set to anything - but:
- The form is still visible
- This is an off the shelf MS control in VS 2005
- I've not disposed or closed the control / form
Stack trace of the error:System.NullReferenceException: Object reference not set to an instance of an object.
at System.Windows.Forms.DataGridViewRowHeaderCell.PaintPrivate(Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, Int32 rowIndex, DataGridViewElementStates dataGridViewElementState, Object formattedValue, String errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts, Boolean computeContentBounds, Boolean computeErrorIconBounds, Boolean paint)
at System.Windows.Forms.DataGridViewRowHeaderCell.Paint(Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, Int32 rowIndex, DataGridViewElementStates cellState, Object value, Object formattedValue, String errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts)
at System.Windows.Forms.DataGridViewCell.PaintWork(Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, Int32 rowIndex, DataGridViewElementStates cellState, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts)
at System.Windows.Forms.DataGridViewRow.PaintHeader(Graphics graphics, Rectangle clipBounds, Rectangle rowBounds, Int32 rowIndex, DataGridViewElementStates rowState, Boolean isFirstDisplayedRow, Boolean isLastVisibleRow, DataGridViewPaintParts paintParts)
at System.Windows.Forms.DataGridViewRow.Paint(Graphics graphics, Rectangle clipBounds, Rectangle rowBounds, Int32 rowIndex, DataGridViewElementStates rowState, Boolean isFirstDisplayedRow, Boolean isLastVisibleRow)
at System.Windows.Forms.DataGridView.PaintRows(Graphics g, Rectangle boundingRect, Rectangle clipRect, Boolean singleHorizontalBorderAdded)
at System.Windows.Forms.DataGridView.PaintGrid(Graphics g, Rectangle gridBounds, Rectangle clipRect, Boolean singleVerticalBorderAdded, Boolean singleHorizontalBorderAdded)
at System.Windows.Forms.DataGridView.OnPaint(PaintEventArgs e)
at System.Windows.Forms.Control.PaintWithErrorHandling(PaintEventArgs e, Int16 layer, Boolean disposeEventArgs)
at System.Windows.Forms.Control.WmPaint(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.DataGridView.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
All Replies
-
Thursday, January 25, 2007 4:57 PM
Sure, of course I've seen that - many times. For me, this mostly happens when I am coming out of a screen saver that is running and most times (but not always) when I am running my app from the IDE. I have not yet looked into in any depth however, because when it happens the call stack is always from external code, never from within the code I developed. The call stack always seems to have something to do with drawing or painting as well and I don't override any of those methods in my app. So I am hoping that it is a problem outside the scope of what I have written. I realize that this may be a false hope, but there are other priorities.
-
Friday, January 26, 2007 8:36 AMOkay - I've fixed a threading problem that was causing a slight issue regarding Null
Exception Reference - But the Big red X is still occurring - Does nobody have any pointers? -
Thursday, February 01, 2007 10:35 AM
Problem solved - After pulling my hair out for almost a week I've resolved the solution - Not the most elegant way, but it works and ensures the red cross of death does not occur...
Add a new UserControl to your solution and change it's inheritance from System.Windows.Forms.UserControl to System.Windows.Forms.DataGridView (If you are using VS2005 or one of the Express versions this will be in the designer class). Next override the OnPaint as detailed below (both c# and vb.net examples
) - HTH any who seem to be having the same problems...
<code=c#>protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
{
try {
base.OnPaint(e);
} catch (Exception ex) {
this.Invalidate();
}
}
</code>
<code=vb>
Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
Try
MyBase.OnPaint(e)
Catch ex As Exception
Me.Invalidate()
End Try
End Sub
</code>- Proposed As Answer by oferfrid Thursday, October 06, 2011 10:30 PM
-
Thursday, February 01, 2007 2:48 PM
Whenever I've seen the red cross it's been because Application.EnableVisualStyles() has been called by application start up code without first checking VisualStyleInformation.IsEnabledByUser == true. The exception is thrown when some controls paint because they attempt to use painting methods that rely on visual styles being enabled - TextBoxRenderer methods for example.
May not be causing your specific problem but it certainly caused mine.
-
Thursday, April 30, 2009 1:15 PM
Probem is probably that after you clear datasource it start repainting datagridview and if you start fill it during it, it throw exception.
I found just one dirty trick:
Before changing data in datasource set visibility of datagridview to false and after it set visibility to true.
Problem is that it not seems very good, there is a little blink. -
Thursday, October 06, 2011 10:30 PM
Problem solved - After pulling my hair out for almost a week I've resolved the solution - Not the most elegant way, but it works and ensures the red cross of death does not occur...
Add a new UserControl to your solution and change it's inheritance from System.Windows.Forms.UserControl to System.Windows.Forms.DataGridView (If you are using VS2005 or one of the Express versions this will be in the designer class). Next override the OnPaint as detailed below (both c# and vb.net examples
) - HTH any who seem to be having the same problems...
<code=c#>
protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
{
try {
base.OnPaint(e);
} catch (Exception ex) {
this.Invalidate();
}
}
</code>
<code=vb>
Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
Try
MyBase.OnPaint(e)
Catch ex As Exception
Me.Invalidate()
End Try
End Sub
</code>
Worked for me TNX!

