ListBox and ComboBox

P

Paul

Is there a bug when trying to bind a data table to ListBox?

After reading just about every post and MSDN article on
data binding to a ListBox and ComboBox (which is suppose
to be the same from everythng I've read) I could not get
the ListBox to accept binding. As soon as I assign a
column of the table to the DisplayMember, the ListBox
would barf with and error message saying you can't change
the collection after assigning the data source to the
ListBox.

On the first try with the comboBox it worked. The ListBox
worked fine with an ArrayList, but of course that doesn't
change dynamically when the ArrayList changes.

Code sample follows:

SqlConnection conn = new +
SqlConnection "server=bullet.francomm.com; uid=paul; +
pwd=oct99sun; database=jewel");

da2= new SqlDataAdapter(classTbl, conn);
da2.Fill(ds, "class");
comboBox1.DataSource= ds;
comboBox1.ValueMember="class.id";
comboBox1.DisplayMember="class.class";

If listBox is used, this code doesn't work (and every
other alternative coding). Anybody have any ideas? or is
this just another bug?

Paul.
 
C

Christopher Kimbell

Hi Paul,

I haven't had any problems using the databinding on ComboBoxes or ListBoxes,
but I have done the following things differently:

1. I set the DataSource to a DataTable, not a DataSet.
2. When setting up the DataBinding, I set the ValueMember and DisplayMember
before setting the DataSource.

I don't know if this should make any difference, but it is worth a try.

Chris
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top