How to bind a Namevaluelist to a combobox?

How to bind a Namevaluelist to a combobox?

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


pirithoos posted on Thursday, September 21, 2006

Used Version : CSLA10vb-060519

It seems that I cannot use a Namevaluelist directly as a DataSource for a ComboBox or ListBox.


me.combobox1.datasource = myNameValueList.GetList


an error is raised : SystemExeption.
complex databinding accepts IList or IListSource as Datasource

(I use a german Version of VS2003 - therefore pls. appologize if the execption is not translated correctly but the meaning should be clear: the DataSource looks for an implementation of IList of IlistSource in the myNameValueList BO)

I do understand this in general, but I would like to know if binding a NVList to a Combobox  is not an intented use of a NameValueList. Reading Rockys book I assumed that using a NVList to populate a ComboBox or ListBox is a typical use?

Do I have to wrapp myNameValueList in another BO (collection e.g.) implementing IList ?

Thanks for your help.

Sarosh replied on Thursday, September 21, 2006

Hi!

I have a similar situation and am not sure what to do.

Have a WinForm with a DataGridView with 20+ columns out of  which 3 columns in the grid are Combobox's.

How many and what type BO's do I need. Here is what I think I should do:

For the MainTable: (Project)

1 BO of type EditableRootList (collection) - ProjectList

1 BO of  type EditableChild - ProjectItem

For LookupTableA (Client)

1 BO's of type NameValueList (to be used in the comboboxcolumn in the grid) - ClientNVL

For LookupTableB (Plant)

1 BO's of type NameValueList (to be used in the comboboxcolumn in the grid) - PlantNVL

For LookupTableC (Area)

1 BO's of type NameValueList (to be used in the comboboxcolumn in the grid) - AreaNVL

Does the above make sense?

I use CodeSmith 2.6 and the CSLA20 templates to generate my BO's but I am using CSLA 2.1 beta. Will this cause other issues?

Thanks

Sarosh

JoeFallon1 replied on Thursday, September 21, 2006

In CSLA 1.x there was no BindableList property for the NVL.

This flaw was discovered early in the release and was overcome by a user who posted the code you need to add to the NVL class in CSLA 1.x in order to make it bindable.

My copy is all hacked up so I won't post it but here are the comments:

#Region " Binding "
  'added by JF on 11/14/2003 for databinding a cbo with a hidden ID and displayed Value.
  'posted by Matt Tag in CSLA forum 11/12/2003
  'Thread = Populating a combo box from CSLA...
  '  Now, from inside your project, you can do this:
  'With cbo
  '   .DataSource = MyCompany.BO.xxxNVL.BindableList
  '   .DisplayMember = "Key"
  '   .ValueMember = "Value"
  'End With

  'In DataBind code add:
  'BindField(cbo, "SelectedValue", mBO, "BO Property")

  'Also - handle the SelectedValueChanged event of the cbo.
  'Forces Databinding to work when cbo is changed and NOT tabbed off of.
  'mBO.Property = cbo.SelectedValue

  Private mBindableList As ArrayList

 ReadOnly Property BindableList....

Do a Search to get the original code.

Joe

 

 

 

Jay Riggs replied on Thursday, February 08, 2007

Joe, thanks for pointing this out.  You saved me a lot of time.

For everyone's reference, here's a link to the code (C# & VB)

 

-Jay

 

Copyright (c) Marimer LLC