comboBox datasource

  • Thread starter Thread starter juli jul
  • Start date Start date
J

juli jul

Hello I want to add data to comboBox from a dataset and doing it this
way:

public OpenDatabase(DataSet ds_db)
{
this.comboBox1=new ComboBox();
this.comboBox1.DataSource=ds_db.Tables[0];
this.comboBox1.DisplayMember = "name";
InitializeComponent();

}

if I am erasing the line :this.comboBox1=new ComboBox()- I get an error
,if I leave it the combobox is empty . How to add the data to it?
Thanks!
 
Juli,

Probably is the reason that you have already set the property datasource and
it prevent to add it double. (maybe in the desinger)

You can set the datasource to null by the way.

Cor
 
Hello,
I checked it and in the design mode there is no value at
DataSource.Maybe there is something besides this that prevents from the
data to be shown in a comboBox?
Thank you!
 
Hi juli jul.
If the combobox is on a form, then InitializeComponent() will instantiate
the combobox to a new combobox, thus wiping out the settings you have given
it. To fix this, move the InitialiseComponent() line to above your code and
remove the line this.comboBox1=newComboBox(). You code should look like:

public OpenDatabase(DataSet ds_db)
{
InitializeComponent();
this.comboBox1.DataSource=ds_db.Tables[0];
this.comboBox1.DisplayMember = "name";
}

By the way, as far as I understand, InitialiseComponent() should only be
called in the form constructor.

HTH
Ron.
 
how about ValueMember??
u must alos set ValueMember and DisplayMember

Ron said:
Hi juli jul.
If the combobox is on a form, then InitializeComponent() will instantiate
the combobox to a new combobox, thus wiping out the settings you have given
it. To fix this, move the InitialiseComponent() line to above your code and
remove the line this.comboBox1=newComboBox(). You code should look like:

public OpenDatabase(DataSet ds_db)
{
InitializeComponent();
this.comboBox1.DataSource=ds_db.Tables[0];
this.comboBox1.DisplayMember = "name";
}

By the way, as far as I understand, InitialiseComponent() should only be
called in the form constructor.

HTH
Ron.
juli jul said:
Hello I want to add data to comboBox from a dataset and doing it this
way:

public OpenDatabase(DataSet ds_db)
{
this.comboBox1=new ComboBox();
this.comboBox1.DataSource=ds_db.Tables[0];
this.comboBox1.DisplayMember = "name";
InitializeComponent();

}

if I am erasing the line :this.comboBox1=new ComboBox()- I get an error
,if I leave it the combobox is empty . How to add the data to it?
Thanks!
 
Back
Top