comboBox datasource

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!
 
C

Cor Ligthert

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
 
J

juli jul

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!
 
R

Ron

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.
 
P

perspolis

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!
 

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