c# - Check for null value in DataGrid View Field when Assigning Variable -


i'm trying assign value string variable when making change datagridview field. unfortunately if it's blank, crashes nullreferenceexception if try check if it's null. don't know of more efficient way write this.

string change = ""; if (dggroups[cid, rid].value.tostring() != null) //gets null reference exception if row/column blank. {     change = dggroups[cid, rid].value.tostring(); } 

your check should this. trying call null.tostring() not valid , throw nullreferenceexception .

string change = ""; if (dggroups[cid, rid].value != null) {     change = dggroups[cid, rid].value.tostring(); } 

if using c# 6.0 can write

string change = dggroups[cid, rid].value?.tostring();//this return null if value null 

Comments

Popular posts from this blog

asynchronous - C# WinSCP .NET assembly: How to upload multiple files asynchronously -

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -