Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Binding a generic List<string> to a ComboBox

I have a ComboBox and I want to bind a generic List to it. Can anyone see why the code below won't work? The binding source has data in it, but it won't fill the ComboBox data source.

FillCbxProject(DownloadData Down)
{
  BindingSource bindingSource = new BindingSource();
  bindingSource.DataSource = Down.ProjectList;
  cbxProjectd.DataSource = bindingSource;
}

On a side note: Is it bad to pass around an instance of a class?

Thanks!

like image 582
Nathan Avatar asked Oct 22 '09 23:10

Nathan


2 Answers

You need to call the Bind method:

cbxProjectd.DataBind();

If this is for winforms then you need to make sure what you have is being called, the following works:

BindingSource bs = new BindingSource();
bs.DataSource = new List<string> { "test1", "test2" };
comboBox1.DataSource = bs;

Although you can set the ComboBox's DataSource directly with the list.

like image 151
Yuriy Faktorovich Avatar answered Sep 28 '22 08:09

Yuriy Faktorovich


this is the simple way (it works correctly):

List<string> my_list = new List<string>();
my_list.Add("item 1");
my_list.Add("item 2");
my_list.Add("item 3");
my_list.Add("item 4");
my_list.Add("item 5");
comboBox1.DataSource = my_list;
like image 45
Bkillnest Avatar answered Sep 28 '22 09:09

Bkillnest