c# combo box populating

  • Thread starter Thread starter JC
  • Start date Start date
J

JC

Hi all,

I have a combobox control in a c# form. I would like to populate this
drop down with values from a dataset- so have written a procedure to
populate a dataset.

Once created I am using this code to populate the combobox...

DataSet ds = new DataSet();
ds = im.getAllOnline();//a function that return a dataset
comboBox1.DataSource = ds.Tables["IM"];
comboBox1.DisplayMember = "username";
comboBox1.ValueMember = "userIp";

I have checked the dataset, and it is being populated, but for some
reason I cannot get the combobox to work.

I would really appreciate help with this cos its driving me nuts!!!
 
Hi James

if it's a webcontrol you have to bind the data to the grid
comboBox1.DataBind();
 
Hi,

You should establish a binding between the combo box and the data source
instead. Please refer to the documentation on the
System.Windows.Forms.Binding class as well as Control.Bindings collection in
MSDN.
 
Thanks for replying Stefan,

The combo box in question is on a windows form, not a web form. For some
reason the control does not have a DataBind() option that you woudl
expect - thats what I'm finding confusing.

Any ideas?

Thanks

JC
 
Hi Dmitriy,

Thanks for the tip I'll read the articles!

I cant link to the datasource direct cos I'm using a seperate layer to
grab the data, and passing it over as a dataset - so I still have a
little problem.

Thanks for the advice though.

JC
 
James,

DataSets and DataTables actually are the most widely used data sources for
the Windows Forms binding - so it's just what you need!
 
Thanks everyone for your help - I think I've cracked it now.

As per Miscro$t site, I went through the dataset and created an
arrayList. I passed this to the combobox and it worked!!
Happy days

Thanks again

JC
 
Back
Top