Combobox Binding Problem

Combobox Binding Problem

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


GinoK posted on Wednesday, September 13, 2006

Hi,

I have recently started developing a system using CSLA and have come up with a problem with Combobox binding I cannot find a solution to.

I have a Business object with some foreign key fields which I am editing through a windows form. One of the fields is a currencyID field where I present a ComboBox  which allows the usere to pick a currency using its description. The Combo box is populated using an NameValue List and displays perfectly. The selectedvalue property is set to the underlying binding source for the BusinessBase object and is being set correctly. However, if I tab from the field the text portion of the combo is cleared and I receive a null conversion error from the error provider. Even though the error is set the underlying property appears to still be set correctly.

The only way I can get around this at present is to include a readonly description field in the root object and reference this in the databindings 'text' field which from other forum discussions should be left blank. Has anyone any ideas where I am going wrong with this?

Thanks in advance for any assistance provided.

Brian Criswell replied on Wednesday, September 13, 2006

Is something bound to ComboBox.Text?  If you are binding to ComboBox.SelectedValue I would normally recommend making sure that nothing is bound to ComboBox.Text.

GinoK replied on Friday, September 15, 2006

I have resolved this by switching from the MS Combobox to the Combobox supplied by Janus. 

I added the Janus Combo box to a form manually. Then I dragged my NameValue List onto the Combo setting up the binding. I switched the DataSourceUpdate mode to OnPropertyChanged for the selected value which defaults to OnValidation. I then cleared the DisplayMember and Value member properties to prevent them being set in the InitializeComponent Sub. (When left the application compained they did not exist event though they were se to expected Key and Value respectively). In my load I then added following lines:

Me.CurrencyNVListBindingSource.DataSource = CurrencyNVList.GetList()

Me.CurrencyIDComboBox.DataSource = CurrencyNVListBindingSource

CurrencyIDComboBox.DisplayMember = "Value"

CurrencyIDComboBox.ValueMember = "Key"

Everything as a result worked fine.  Now who was that who told me there was no need for third party tools with Visual Studio 2005?

 

Regards,

 

 

david.wendelken replied on Sunday, September 17, 2006

I do the same thing with the regular MS DropDownList control.

GinoK replied on Monday, September 18, 2006

David,

I would expect that this is a regular requirement and expect I am doing something stupid. However, I am getting nowhere on support sites. Using the MS Dropdown I get the following properties in designer.

'MSCurrencyIDComboBox1

'

Me.ReadWriteAuthorization1.SetApplyAuthorization(Me.MSCurrencyIDComboBox1, False)

Me.MSCurrencyIDComboBox1.DataBindings.Add(New System.Windows.Forms.Binding("SelectedValue", Me.ControlCompanyBindingSource, "CurrencyID", True, System.Windows.Forms.DataSourceUpdateMode.OnPropertyChanged))

Me.MSCurrencyIDComboBox1.DataSource = Me.CurrencyNVListBindingSource

Me.MSCurrencyIDComboBox1.DisplayMember = "Value"

Me.MSCurrencyIDComboBox1.FormattingEnabled = True

Me.MSCurrencyIDComboBox1.Name = "MSCurrencyIDComboBox1"

Me.MSCurrencyIDComboBox1.ValueMember = "Key"

 

On my own Initialization of the component I have:

Me.CurrencyNVListBindingSource.SuspendBinding()

Me.CurrencyNVListBindingSource.DataSource = CurrencyNVList.GetList()

MSCurrencyIDComboBox1.DataSource = CurrencyNVListBindingSource

MSCurrencyIDComboBox1.DisplayMember = "Value"

MSCurrencyIDComboBox1.ValueMember = "Key"

Me.ControlCompanyBindingSource.ResumeBinding()

What I am trying to find out is what I am missing. Selected value is being set fine and forwarded to B.O.  but display is blank on attempting to leave the combo control.

 

Janus control got following from designer and my initialization similar to MS control above.

Me.CurrencyIDComboBox.ComboStyle = Janus.Windows.EditControls.ComboStyle.DropDownList

Me.CurrencyIDComboBox.DataBindings.Add(New System.Windows.Forms.Binding("SelectedValue", Me.ControlCompanyBindingSource, "CurrencyID", True, System.Windows.Forms.DataSourceUpdateMode.OnPropertyChanged))

Me.CurrencyIDComboBox.Name = "CurrencyIDComboBox"

Again any help in identifying what the problem is would be appreciated. I would prefer not to use the Janus COntrol if unnecessary as I prefer additional properties available in MS control.

 

Regards

Brian Criswell replied on Monday, September 18, 2006

You seem to be doing lots of extra steps.  The designer is setting up all of your bindings for you.  I would think that the only line of code you would need to write is:

Me.CurrencyNVListBindingSource.DataSource = CurrencyNVList.GetList()

That's how it works for me at least.

GinoK replied on Friday, September 22, 2006

I solved my problem. I previously wrote a code generator for building my classes from an Informix database. The primary keys in the database are mostly system allocated similar to an identity key in SQL Server. I translated this to a Long type in my classes which would be also be the  type on all foreign keys referencing these tables.  The NameValueList is  defined as having a key of integer which while the system had no problem populating the class the selectedItem on the BoundCombo failed to display correctly. By changing my Foreign keys in my underlying datasource to Integer, Int32 or int16 all my binding issues were resolved.

 

Thanks to all those who tried to assist me.

 

Regards,

 

GinoK

Copyright (c) Marimer LLC