Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
1705
Allow null with DropDownValidate
posted

Hey guys,

I need a combo box that validates the value entered and also it allows empty values.

DropDownValidate seems to do the trick since it offers type-ahead functionality and it also verifies the value entered is part of the data source.

The problem is that even when the underlying column type is string (obviously allows nulls) and also an empty item (and a null item) has been added to the list, the grid keeps showing the following message:

 

"Unable to update the data value: Value in the editor is not valid"

 

Is this expected behavior? so does DropDownValidate definitely not like nulls?

I read somewhere that a solution is to validate manually with the BeforeCellUpdate event. So I was writing to see if this the "best and easy" way to do it. Since it looks to me that the DropDownValidate style has a silly limitation.

 

Thanks.

  • 45049
    Suggested Answer
    posted

    ureys84,

    ureyes84 said:
    Is this expected behavior?

    If the data in your list doesn't have something that corresponds to a null value, then yes, this is expected behavior.  DropDownValidate requires that the entry made by the user exists in the list, and if you don't have a "null" entry in your list, it will be rejected.

    There are two options:

    • Add an entry with an empty string to the list for your dropdown, matching to a value of null (or DBNull.Value, depending on the datatype of the column as exists your grid's data source).
    • Instead of using DropDownValidate, use DropDown instead, and validate the entry yourself in the BeforeExitEditMode event.  If the user's entry isn't null and doesn't match an item in the list, cancel the event - this will keep the cell in edit mode.  Be sure to not use a MessageBox in this event, or you'll force the cell out of edit mode anyway.