Null handling in cells for datagridview
I need a quick lesson on doing this the right way.
I have a datagridview (dgv) to contain a list (phones). Let's say a phone has 2 properties: PhoneNum and PhoneExt. Both are string.
Now, when it comes to saving the data, it processes each gridrow and checks that PhoneNum is not null. If not, it's time to save the new phone.
The code build a list (phones) from dgv. It sets phones[row].PhoneExt to dgv[row]["PhoneExt"].Value.ToString().
If user does not enter an Extension, the cell is null, and the process throws an exception.
The reason this occurs is obvious to me. Clearly, I could check every column in a datagridview for != null, but this seems like a waste and an overburden - especially when considering that the more properties (cells) you have, the more null-checking you have to do. I am thinking I do not know enough about design to where this should not be required.
.... or do programmers really check every cell for null all the time?
P.S. I also know I could write a function to accept the value of anything, and if null, return "". Ex. phones[row].PhoneExt = checkValue(dgv[row]["PhoneExt"].Value) -> where checkValue will return "" if null, or Value.ToString() if not null.
But even here, it just seems like it is an ugly way to do it.
How is this process designed properly?
Re: Null handling in cells for datagridview
Yes, every time you are going to use a dataGridView.CurrentCell.Value, you should in-fact check to see that it is not null.
Here is one way of doing it:
Code:
String sval = (String)dataGridView.CurrentCell.Value;
if (sval != null)
{
//use sval
}
Another way of doing it is using Microsoft Sanctioned Methods:
http://msdn.microsoft.com/en-us/libr...nullvalue.aspx
Re: Null handling in cells for datagridview
you could just make your own rapper for string and use it instead to set null to ""
Re: Null handling in cells for datagridview
Re: Null handling in cells for datagridview