CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Dec 2011
    .Net 4.0

    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?

  2. #2
    Join Date
    Mar 2001

    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:
    String sval = (String)dataGridView.CurrentCell.Value;
    if (sval != null)
      //use sval
    Another way of doing it is using Microsoft Sanctioned Methods:
    To keep the plot moving, that's why.

  3. #3
    Join Date
    Oct 2012

    Re: Null handling in cells for datagridview

    you could just make your own rapper for string and use it instead to set null to ""

  4. #4
    Join Date
    Oct 2012

    Re: Null handling in cells for datagridview


  5. #5
    Join Date
    Oct 2012

    Re: Null handling in cells for datagridview

    See this link http://a2zsollution.com/

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

On-Demand Webinars (sponsored)

We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.