Results 1 to 4 of 4

Thread: [RESOLVED] Correcting null values in boolean columns

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Oct 2011
    Posts
    152

    Resolved [RESOLVED] Correcting null values in boolean columns

    I've got a datagridview populating from a database and, there's no way around it, the database will sometimes return null values in a boolean column, and I get the following error:

    The following exception occurred in the DataGridView:

    System.FormatException: Value '' cannot be converted to type 'Boolean'.
    at System.Windows.Forms.Formatter.FormatObject(Object value, Type targetType,
    TypeConverter sourceConverter, TypeConverter targetConverter, String formatString,
    IFormatProvider formatInfo, Object formattedNullValue, Object dataSourceNullValue)
    at
    System.Windows.Forms.DataGridViewCell.GetFormattedValue(Object value,
    Int32 rowIndex, DataGridViewCellStyle& cellStyle, TypeConverter valueTypeConverter,
    TypeConverter formattedValueTypeConverter, DataGridViewErrorContexts context)

    To replace this default dialog please handle the DataError event.
    I tried handling this in the Cellvalidating event, but it didn't work. How do I actually catch those nulls before they cause an error?

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Correcting null values in boolean columns

    The column supports that situation without any extra code from you.

    http://msdn.microsoft.com/en-us/libr...hreestate.aspx
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    Re: Correcting null values in boolean columns

    As the error occurs in the situation when you want to put "something" (of type whatever) into a position (like a variable, datagridcell ...) where a specific type is expected, I would check the type of "something" just in this situation. If it doesn't fit you can give a DEFAULT value.
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Oct 2011
    Posts
    152

    Re: Correcting null values in boolean columns

    (I just went ahead and changed null values to false in the cellformatting event.)

Posting Permissions

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



Click Here to Expand Forum to Full Width