code dataGridView1[e.ColumnIndex, e.RowIndex].Value.ToString() doesnt work
-
Dear Experts, I am trying to get the value for current cell in DatagridView. I wrote code
dataGridView1[e.ColumnIndex, e.RowIndex].Value.ToString();
in CellLeave event. but the code raise an error. Object reference not set to an instance of an object. But if I write the code in Button_Click event, it is working properly. Please help. Ahmad
-
Dear Experts, I am trying to get the value for current cell in DatagridView. I wrote code
dataGridView1[e.ColumnIndex, e.RowIndex].Value.ToString();
in CellLeave event. but the code raise an error. Object reference not set to an instance of an object. But if I write the code in Button_Click event, it is working properly. Please help. Ahmad
Use the debugger. When you get the exception, use the debugger to look at the various parts of that, and work out what the values of each part are: e.ColumnIndex, e.RowIndex, and so forth. Personally, I'd be using dataGridView1.Rows[rowindex].Cells[columnIndex] and checking the return value to make sure there is one. I suspect you have just mixed up the row and column, and been lucky not to get an index out of range error. But the debugger is the best way to find out.
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
-
Use the debugger. When you get the exception, use the debugger to look at the various parts of that, and work out what the values of each part are: e.ColumnIndex, e.RowIndex, and so forth. Personally, I'd be using dataGridView1.Rows[rowindex].Cells[columnIndex] and checking the return value to make sure there is one. I suspect you have just mixed up the row and column, and been lucky not to get an index out of range error. But the debugger is the best way to find out.
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
Thank you Sir, I have tried your code, but I get the same error. Below is the exception message I received. The code works under Button_Cleck event.
System.NullReferenceException was unhandled
Message=Object reference not set to an instance of an object.
Source=ERP
StackTrace:
at ERP.frmCalendar.dataGridView1_CellLeave(Object sender, DataGridViewCellEventArgs e) in C:\Projects\ERP\ERP\GL\frmCalendar.cs:line 103
at System.Windows.Forms.DataGridView.OnCellLeave(DataGridViewCellEventArgs e)
at System.Windows.Forms.DataGridView.OnCellLeave(DataGridViewCell& dataGridViewCell, Int32 columnIndex, Int32 rowIndex)
at System.Windows.Forms.DataGridView.CommitEdit(DataGridViewCell& dataGridViewCurrentCell, DataGridViewDataErrorContexts context, DataGridViewValidateCellInternal validateCell, Boolean fireCellLeave, Boolean fireCellEnter, Boolean fireRowLeave, Boolean fireRowEnter, Boolean fireLeave)
at System.Windows.Forms.DataGridView.EndEdit(DataGridViewDataErrorContexts context, DataGridViewValidateCellInternal validateCell, Boolean fireCellLeave, Boolean fireCellEnter, Boolean fireRowLeave, Boolean fireRowEnter, Boolean fireLeave, Boolean keepFocus, Boolean resetCurrentCell, Boolean resetAnchorCell)
at System.Windows.Forms.DataGridView.CommitEditForOperation(Int32 columnIndex, Int32 rowIndex, Boolean forCurrentCellChange)
at System.Windows.Forms.DataGridView.ScrollIntoView(Int32 columnIndex, Int32 rowIndex, Boolean forCurrentCellChange)
at System.Windows.Forms.DataGridView.TabToNextCell()
at System.Windows.Forms.DataGridView.ProcessTabKey(Keys keyData)
at System.Windows.Forms.DataGridView.ProcessDialogKey(Keys keyData)
at System.Windows.Forms.Control.ProcessDialogKey(Keys keyData)
at System.Windows.Forms.TextBoxBase.ProcessDialogKey(Keys keyData)
at System.Windows.Forms.Control.PreProcessMessage(Message& msg)
at System.Windows.Forms.Control.PreProcessControlMessageInternal(Control target, Message& msg)
at System.Windows.Forms.Application.ThreadContext.PreTranslateMessage(MSG& msg)
at System.Windows.Forms.Application.ThreadContext.System.Windows.Forms.UnsafeNativeMethods.IMsoComponent.FPreTranslateMessage(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.Thread -
Dear Experts, I am trying to get the value for current cell in DatagridView. I wrote code
dataGridView1[e.ColumnIndex, e.RowIndex].Value.ToString();
in CellLeave event. but the code raise an error. Object reference not set to an instance of an object. But if I write the code in Button_Click event, it is working properly. Please help. Ahmad
Hi, when you get "Object reference not set to an instance of an object" exceptions some of the properties you invoke have returned
null
, in this case most likelyValue
for an empty grid cell. Check the Value for null before using it:var gridValue = dataGridView1[e.ColumnIndex, e.RowIndex].Value;
if( gridValue != null )
{
string gridStringValue = gridValue.ToString();
...
} -
Thank you Sir, I have tried your code, but I get the same error. Below is the exception message I received. The code works under Button_Cleck event.
System.NullReferenceException was unhandled
Message=Object reference not set to an instance of an object.
Source=ERP
StackTrace:
at ERP.frmCalendar.dataGridView1_CellLeave(Object sender, DataGridViewCellEventArgs e) in C:\Projects\ERP\ERP\GL\frmCalendar.cs:line 103
at System.Windows.Forms.DataGridView.OnCellLeave(DataGridViewCellEventArgs e)
at System.Windows.Forms.DataGridView.OnCellLeave(DataGridViewCell& dataGridViewCell, Int32 columnIndex, Int32 rowIndex)
at System.Windows.Forms.DataGridView.CommitEdit(DataGridViewCell& dataGridViewCurrentCell, DataGridViewDataErrorContexts context, DataGridViewValidateCellInternal validateCell, Boolean fireCellLeave, Boolean fireCellEnter, Boolean fireRowLeave, Boolean fireRowEnter, Boolean fireLeave)
at System.Windows.Forms.DataGridView.EndEdit(DataGridViewDataErrorContexts context, DataGridViewValidateCellInternal validateCell, Boolean fireCellLeave, Boolean fireCellEnter, Boolean fireRowLeave, Boolean fireRowEnter, Boolean fireLeave, Boolean keepFocus, Boolean resetCurrentCell, Boolean resetAnchorCell)
at System.Windows.Forms.DataGridView.CommitEditForOperation(Int32 columnIndex, Int32 rowIndex, Boolean forCurrentCellChange)
at System.Windows.Forms.DataGridView.ScrollIntoView(Int32 columnIndex, Int32 rowIndex, Boolean forCurrentCellChange)
at System.Windows.Forms.DataGridView.TabToNextCell()
at System.Windows.Forms.DataGridView.ProcessTabKey(Keys keyData)
at System.Windows.Forms.DataGridView.ProcessDialogKey(Keys keyData)
at System.Windows.Forms.Control.ProcessDialogKey(Keys keyData)
at System.Windows.Forms.TextBoxBase.ProcessDialogKey(Keys keyData)
at System.Windows.Forms.Control.PreProcessMessage(Message& msg)
at System.Windows.Forms.Control.PreProcessControlMessageInternal(Control target, Message& msg)
at System.Windows.Forms.Application.ThreadContext.PreTranslateMessage(MSG& msg)
at System.Windows.Forms.Application.ThreadContext.System.Windows.Forms.UnsafeNativeMethods.IMsoComponent.FPreTranslateMessage(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadSo use the debugger and find out which bit is null. Then you can look at why! But I can't do that for you - I have no access to your computer...
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
-
Dear Experts, I am trying to get the value for current cell in DatagridView. I wrote code
dataGridView1[e.ColumnIndex, e.RowIndex].Value.ToString();
in CellLeave event. but the code raise an error. Object reference not set to an instance of an object. But if I write the code in Button_Click event, it is working properly. Please help. Ahmad
string cellval = "";
if(dataGridView1[e.ColumnIndex, e.RowIndex].Value != null){
cellval = dataGridView1[e.ColumnIndex, e.RowIndex].Value.ToString();
}I usually use Convert.ToString(...); in such cases, because it will convert a null to "" automatically. (not useful in all cases)
V.
(MQOTD rules and previous solutions)