October 5th, 2012, 08:11 AM
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?
Click Here to Expand Forum to Full Width
This a Codeguru.com survey!