Click here to Skip to main content
16,005,682 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Running DataGridView with VirtualMode=true.
Attempted to change cell type from DataGridViewCheckBoxCell to DataGridViewTextBoxCell for a specific cached value in the EventHandler CellValueNeeded. This causes an UnhandledException. Please help! See details below.

Code-----------------------------------------------------------------------------
dgvCell = new DataGridViewTextBoxCell();
dgvCell.Value = string.Empty;
dgvApproval[e.ColumnIndex, e.RowIndex] = dgvCell;


Exception------------------------------------------------------------------------
Object reference not set to an instance of an object.

Inner Exception------------------------------------------------------------------
Null

Stack Trace----------------------------------------------------------------------
at System.Windows.Forms.DataGridViewCell.GetEditedFormattedValue(Object value, Int32 rowIndex, DataGridViewCellStyle& dataGridViewCellStyle, DataGridViewDataErrorContexts context)
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.PaintCells(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.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at Approval.Program.Main() in C:\Users\tteelucksingh\Documents\Approval 2010\Approval\Approval\Program.cs:line 41
Posted

1 solution

Perhaps this is not all of the relevant code. It looks like you are trying to fill all the cells with empty strings. First, it does not seem to make sense. Also, you are not supposed to create any cells at all. Instead, you can should have assigned e.Value = String.Empty; using your event arguments parameter.

This is good to show the complete event handler. It's also good to show where you add your handler to the event's invocation list. If you did it using Designer can make some excuse but not when you post code to ask a question. That said, it would be much better to create a special short but completely buildable code sample just for asking a question. It could help you to find the solution by yourself.

Also, always use the Debugger to see where the exception is thrown and propagated before asking such question. It also could help you to fix things by yourself.

—SA
 
Share this answer
 
v2
Comments
MatLeave 31-Jan-12 8:47am    
The CellValueNeed event handler is called to populate the datagridview using e.value = cache(e.RowIndex, e.ColumnIndex). One of the columns of the datagridview is predefined as a DataGridCheckBoxColumn. I populate the column with true or false values dependent on the cached value. This all works fine!

In some cases I do not want to display a checkbox; I would like to change the cell from DataGridCheckBoxCell to DataGridTextBoxCell, set it to empty string and mark as readonly. After much testing and debugging I track the error to the line dgvApproval[e.ColumnIndex, e.RowIndex] = dgvCell. That is, where the datagridviewcell is being referenced. GetEditedFormattedValue is throwing a NullReferenceException. With more debugging I tried to return the value of dgvApproval[e.ColumnIndex, e.RowIndex].FormattedValue, which returned Null. From my understanding this value is null when the DataGridViewCell does not belong to a DataGridView. (Ref: http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewcell.formattedvalue.aspx). At this point I am stuck. Any suggestions?

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