Click here to Skip to main content
15,896,522 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Hi;

I get the above exception in this code line.

DataGridView1.Rows[iIndex].Cells[1].Value = iGLCount.ToString();

datagridview is defined in the Designer.cs i dont think i need the Troubleshooting tip "Use the 'new' keyword to create an object instance"
in some forums i did read that i can not modify/set cells directly so i shoul enable the edit mode like ;

DataGridView1.BeginEdit(); and i got nada.

Any ideas ?
Posted
Updated 27-Feb-11 8:51am
v2
Comments
Sandeep Mewara 27-Feb-11 14:34pm    
Good to know that my answer helped. :)


1 solution

System.NullReferenceException
This simply means that you are trying to use a property of an object which is actually a NULL.


Now, looking at the line you say:
MIDL
DataGridView1.Rows[iIndex].Cells[1].Value = iGLCount.ToString();

Potential objects, which can be easily debugged, seen and rectified are:
Check sequentially:
1. iGLCount : Is this object NULL? If so, doing a ToString() would throw an error
2. DataGridView1.Rows[iIndex]: Does this return a valid row object? If yes, move ahead for third and last one
3. DataGridView1.Rows[iIndex].Cells[1] : Does this return a proper object? If not, and is a NULL, then doing a .Value would throw an error.

Now, just check which one of it is (1 or 2 or 3) and handle the same. Have a NULL check before using it.
 
Share this answer
 
Comments
Sandeep Mewara 27-Feb-11 14:33pm    
Comment from OP:
Thanks Sandeep,

i assumed that i could set the cell.value directly which i cant.
DataGridView1.Rows[iIndex].Cells[1].Value is null so throws an error.
Dave Paras 27-Feb-11 23:46pm    
Nice and Sufficient information.

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