Databinding woes

Databinding woes

Old forum URL: forums.lhotka.net/forums/t/1730.aspx


ajj3085 posted on Thursday, November 09, 2006

Hi,

I have a combobox.  It allows a user to pick a selection from the list, or type in data.

The Value is bound to my business object, which exposes an integer property.  I do some trickery in the event handlers so that if the user types something not in the list, the Id is set to int.MaxValue to 'trick' the BO into thinking is valid (the BO has a rule stating the int must be > -1).

Everything works fine...except I can open the list, close it by clicking the down arrow again.  Then, when I attempt to move off, my ErrorProvider blinks and says 'Cant convert System.DBNull to System.Int32'  Now, I DO set the Null Value in the advanced options of the combobox's Databindings to -1.

Does anyone know why databinding is still trying to force DBNull into my int property, even though I told it the value to use?  My TextChanged and SelectedIndexChanged events for the combo don't fire either.

Thanks
Andy

Bayu replied on Thursday, November 09, 2006

The NullValue is just the value that gets displayed in the control in case the user types an empty string or a null-value, the DataSourceNullValue is the one pushed to your BO .....

Is usually set the NullValue to something like '<Select>' to indicate nothing was selected yet.

The DataSourceNullValue is set to DBNull by default, so I guess that when you set it to Integer.MaxValue all should be fine.

Bayu

ajj3085 replied on Friday, November 10, 2006

Ok, where do I set the DataSourceNullValue?

Bayu replied on Friday, November 10, 2006

Hi Andy,

The following gives the idea:

        Dim binding As New Binding("SelectedValue", someBindingSource, somePropertyInfo.Name, True)
        binding.DataSourceUpdateMode = DataSourceUpdateMode.OnPropertyChanged
        binding.DataSourceNullValue = Guid.Empty
        binding.NullValue = "<Select>"

        Dim combo As New ComboBox
        combo.DropDownStyle = ComboBoxStyle.DropDown
        combo.DataBindings.Add(binding)

In addition you would set the DataSource of the combobox to some list that contains the values you want the user to choose from.

        combo.DataSource = someDataSource

Having the combobox setup like this will yield the following behavior:
- empty values will result in a Guid.Empty being pushed to your BO behind the databinding
- and the combobox will display the text '<Select>' to notify that a selection still needs to be made
- the DataSourceUpdateMode.OnPropertyChange is something I use sometimes to ensure that the values are pushed to the BO already upon Change, instead of only after the user leaves (and thereby validates) the comobobox.

Bayu

ajj3085 replied on Friday, November 10, 2006

Bayu,

Thanks for the tip.  I'll check that out..  Its odd that you can't set that via the designer somehow.

Andy

Bayu replied on Friday, November 10, 2006

You're welcome. ;-)

I too only got to find this out because IntelliSense popped it up in the list. Much of the more advanced stuff is hidden from the Designer, just to make life 'easy' I guess ..... Tongue Tied [:S]

Hope it works.

Bayu

ajj3085 replied on Friday, November 10, 2006

Bayu,

I made the changes you suggested, and it looks like its working perfectly. 

It is a shame its not in the designer, espcially since NullValue is, and I (incorrectly) thought NullValue was what I wanted to change... so it was misleading.

Thanks again!
Andy

Copyright (c) Marimer LLC