Hi All,
I am having a problem when I try to apply the method
string.IsNullorEmpty()
over an object, in my case, a dataGridView Cell.
My method verifies 2 cells of each line of a dataGridView and check if both Cells are filled if one of them is.
The problem is that when I use the code below...
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
if(!string.IsNullOrEmpty(dataGridView1.Rows[i].Cells["Column1"].Value.ToString()) && string.IsNullOrEmpty(dataGridView1.Rows[i].Cells["Column2"].Value.ToString()))
{
MessageBox.Show("Please, enter a valid value for each description.");
return;
}
if(!string.IsNullOrEmpty(dataGridView1.Rows[i].Cells["Column2"].Value.ToString()) && string.IsNullOrEmpty(dataGridView1.Rows[i].Cells["Column1"].Value.ToString()))
{
MessageBox.Show("Please, enter a valid description for each value.");
return;
}
}
I have the error
Object reference not set to an instance of an object..
I know why it happens, but, I can't verify this cell using only
!string.IsNullOrEmpty(dataGridView1.Rows[i].Cells["Column2"].Value)<br />
... I have to use
.ToString()
after
.Value, but, when I execute the code, it can't convert a
null value to a string for it to use the method
string.IsNullorEmpty()
.
How can I make it happens without have to error
Object reference not set to an instance of an object.?
Thanks is advance! :)