Strange databound combo box behaviour

Strange databound combo box behaviour

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


gajit posted on Wednesday, November 01, 2006

I have a problem that I'm sure I've come across before and worked around it, but for the life of me cannot figure out what the problem is.

I have a windows form with a number of textboxes and a couple of combo boxes. All the textboxes are bound to my BO (businessbase) as are the comboboxes advanced databinding properties. The datasource for the 2 comboboxes are bound to a NameValueList and a ReadOnlyListBase respectively.

I am also using a bindingsourcerefresh and Errorprovider on my form.

My problem appears to be with the NameValueList source.

When the form initially loads. The dropdownlist has a value in it (the first in the list). I have a number of brokenrules on my form by default, but as soon as I correct one of them ( value in a text box), and navigate off of the control, the dropdownlist value disappears.

I also never receive a error icon against the control when it is empty - I don't know if that's normal behaviour or not, even though I have a validation rule (stringrequired).

Any ideas?

Thanks,
Gaj.





Bayu replied on Thursday, November 02, 2006

Are the comboboxes bound to your BO with their SelectedValue/SelectedIndex?

I had this issue when my BO had a Guid.Empty by default, but in my namevaluelist there was no corresponding entry ....


Bayu

gajit replied on Thursday, November 02, 2006

The combobox SelectedValue is where I have my binding to the BO.

So, to clarify...

combobox.Datasource = BranchListBindingSource (NameValueList)
combobox.DisplayMember = "Key"
combobox.ValueMember = "Key"
combobox.Databindings.SelectedValue = Customer_BindingSource - BRANCH

There doesn't appear to be anything wrong...
By default, the BRANCH column on the BO will be empty... could this be the problem? And the combobox. because it is a DropdownList type HAS to have a value in it? If so, I'm still not understanding why the errorprovider icon doesn't dispay next to the control if the validationrule is broken...

Any help would be appreciated.

Thanks,
Gaj.





ajj3085 replied on Thursday, November 02, 2006

What does your property get / set look like?  Are you sure you're storing the value in the proper field?  Are you sure the property is not bound to another control on the form?

gajit replied on Thursday, November 02, 2006

Hey ajj..

Fixed it I believe... or least found the workaround...

Everything looks good in the property Sets/Gets - and no other controls on the form bind to the BRANCH column.

HOWEVER, I have found the reason for no error provider - I didn't have my validationrule in place. Soooo, I have added this, and it appeared to give a little more understanding to what is happening.

I would have expected that the bo.BRANCH  column would be updated by whatever item is currently set in the combobox, or that the combobox would display an empty value. But it appears that the first combobx entry was being displayed, but not actually binding it's value to the BO...

When the form was being opened, I was instantiating my BO binding in the New() method and the combobox datasource in the _Load event...

I moved the combobox datasource instantiate into the New() method BEFORE the BO instantiate and it appears to work fine.

        Me.BranchListBindingSource.DataSource = BranchList.GetList
        Me.CustomerBindingSource.DataSource = mCustomer

Not sure WHY this would have an impact, but I'm happy... (for now...)

Thanks,
Gaj.





ajj3085 replied on Thursday, November 02, 2006

Ahh, I think what was happening is that you assigned the BO to the binding source, the assign the list to its binding source... which causes the combo to load, which causes a list changed event, which may or may not be putting the value into the BO.  Then things get out of sync.

Devi replied on Monday, March 31, 2008

I am using the version 2.1.4

I have a combobox in an editable grid.. Its datasourse property is set to a NamevalueListBase class and DispalyMember, ValueMember are also set..And Its selecetedValue property is bind to tthe CourseID in the BusinessObject Course. In the UI, i use like these.. bindingSourseCourse.Datasourse = CourseNVL();

// I set the DataSourse of  bindingSourseCourse as CourseNVL in the design.

bt while opening this form, i get an error in the combobox like this..

"DataGridView DefaultError Dialog

System.ArguementException : DataGridViewComboboxCell value is not valid.

To replace this default Dialog please handle the DataError event"

But when i initialize value for the CourdeID in the BO, it works properly.. But i want to set the Combobox null while loading the form..

How can i do that...

Bayu replied on Thursday, November 02, 2006

gajit:


By default, the BRANCH column on the BO will be empty... could this be the problem? And the combobox. because it is a DropdownList type HAS to have a value in it? If so, I'm still not understanding why the errorprovider icon doesn't dispay next to the control if the validationrule is broken...



This could definitely be the problem. I am not saying it actually is, but this greatly resembles the issue I faced with my default Guid.Empty value that did not have an entry in my NVL. I guess the error provider is not shown because there is not really an error ..... you just set up your UI in a inconvenient way such that you cannot edit the BRANCH as you like.

On my NVLs I tend to make a shared GetDefaultValueID property as follows (example taken from a StatusNVL):

    Public Shared Function GetDefaultStatusID() As Integer
        Dim list As StatusNVL = GetList()
        If list.Count > 0 Then
            Return list.Items(0).Key
        Else
            Throw New NullReferenceException("No statuses available; default Status can not be returned")
        End If
    End Function

This function is sued in the dp_create of my BOs to initialize a status-id.

Bayu


Copyright (c) Marimer LLC