-
October 5th, 2012, 08:11 AM
#1
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?
-
October 5th, 2012, 11:42 AM
#2
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
ahoodin
To keep the plot moving, that's why.
-
October 7th, 2012, 10:25 AM
#3
Re: Null handling in cells for datagridview
you could just make your own rapper for string and use it instead to set null to ""
-
October 7th, 2012, 10:25 AM
#4
Re: Null handling in cells for datagridview
-
October 9th, 2012, 07:00 AM
#5
Re: Null handling in cells for datagridview
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|