DataGridView Handle Null Value

Hi,

I have a for loop that iterates through a datagrid to verify all cells are formatted correctly, one of the validations is checking that the 1st column on every row (Description) is entered and at least 10 characters.

In the for loop i have the following

		$descriptionLength = ($datagridview1.Rows[$i].Cells[0].Value).ToString().Length
		If ($descriptionLength -le 9)
		{
			$validated = $false
			$richtextbox1Errors.Visible = $true
			$richtextbox1Errors.AppendText("  ERROR: Line $($i+1) - Description should have at least 10 characters.$nl")
		}

This works if i have something entered but if the cell is blank i get the error “You cannot call a method on a null-valued expression.”

I have tried a couple different ways and can’t seem to find a way to handle a null value, the error is complaning about the line setting the variable rather than the if statement.

I did previous try without setting a variable as per this:

		If ((($datagridview1.Rows[$i].Cells[0].Value).ToString()).Length -le 9)
		{
			$validated = $false
			$richtextbox1Errors.Visible = $true
			$richtextbox1Errors.AppendText("  ERROR: Line $($i+1) - Description should have at least 10 characters.$nl")
		}

But that too had the say null value error

Any help will be muchly appreciated.

Regards,
Jamie

Jamie,

it’s hard to reproduce the issue without any data to play with. But I think your if statement checks the wrong property. I’d try to check if there is a value at all before you can measure the length.

if ($datagridview1.Rows[$i].Cells[0].Value)
{
    "...."
}
1 Like

Hi,

This is actually what im trying to do, the above examples work when something exists, i.e. when i have 1 to 9 characters however, when there is nothing (null) i don’t know how to check for that.

How do i check a datagridview cell to see if its null?

I also tried

if ($datagridview1.Rows[$i].Cells[0].Value -eq $null)
{
    "...."
}

That didn’t work either, still got the error “You cannot call a method on a null-valued expression.”

Thank you

hmmm … it may sound picky but Nothing NULL !!! :point_up_2:t4: :point_up_2:t4: NULL is a valid value. That’s why I recommended to check if there’s a value at all.

AND if you want to check for NULL $NULL should be on the left hand side of the comparison.

I’d think it’s still worth a try to check exactly as I recommended. :wink:

1 Like

Hi Olaf,

Thank you! the issue was i was looking for the null value second, i.e. i was checking if <9 first then saying -or null.

I switched that around and it’s now working, i didn’t realise null had to be evaluated first but can see why thats the case now :slight_smile:

Thank you for pointing that out.

Regards,
Jamie

I’m afraid you did not get what I tried to explain.

Again: “Nothing” is NOT EQUAL to “NULL” !!! NULL is a valid value. If a variable contains the vaule “NULL” it is not empty. If your code works like this at the moment it may not work anymore if there is really an empty cell. That’s why there are some methods called .IsNullOrEmpty(). That are two different states - Null OR Empty !!

Hi Olaf,

Thank you for re-iterating your point, although my script started working with the change in order of the operator, i did some further reasearch to understand your point more and hopefully i understand what you mean by $null not meaning “nothing”

So where my understanding was wrong is… (I think)

I was checking if the cell is not equal to $null, thinking $null would be “Nothing” and therefore if it is not equal to $null then it must have a something entered.

If ($datagridview1.Rows[$i].Cells[0].Value -ne $null)
{
	...
}

But it could actually be equal to $null where $null is a value and therefore give unexpected results in some scenarios?

I think in my case this shouldn’t be an issue but…

Replacing the IF statement to do as you suggested, where it checks to see if there is ANY value at all first and then checking the legnth is less than 10 characters would be the safer way?

2 IF’s nested might not be the most elegant way, maybe they can be combined into one but…

If ($datagridview1.Rows[$i].Cells[0].Value)
{
	if (($datagridview1.Rows[$i].Cells[0].Value.ToString()).Length -le 9)
			{
				...
			}
} else { ... }

Hopefully this is me understanding it a little better :slight_smile:

Thank you for your help.

That’s exactly what I meant. :+1:t4: :+1:t4: :wink:

And that’s exactly the result I was hoping for. That you at least consider this case for your particular situation. :+1:t4: :+1:t4: :wink: