Dataset -> combobox?

L

Lasse Edsvik

Hello

i've selected data from a table and put it into a dataset, now how do i
populate a combobox the easiest way?

OleDbDataAdapter DA = new OleDbDataAdapter();

OleDbCommand SQL = new OleDbCommand("SELECT * FROM Products", conn);

DataSet DS = new DataSet();

DA.SelectCommand = SQL;


DA.Fill(DS,"Products");

comboBox1.DataSource = DS; // no no no nooooooooooo :)
 
S

Steve Lutz

Lasse

You have it almost correct.

[ Database stuff here]

Combobox1.Datasource = DS.Tables["Products"];
Combobox1.DataTextField = "ProductName"; // I am guessing at column
name
Combobox1.DataValueField = "ProductID"; // Same here
Combobox1.DataBind();
 
L

Lasse Edsvik

Steve,

hmm, i dont get any properties/methods DataTextField, DataValueField and
DataBind

only get up Datasource after hitting . after comboBox1

/Lasse


Steve Lutz said:
Lasse

You have it almost correct.

[ Database stuff here]

Combobox1.Datasource = DS.Tables["Products"];
Combobox1.DataTextField = "ProductName"; // I am guessing at column
name
Combobox1.DataValueField = "ProductID"; // Same here
Combobox1.DataBind();



Lasse Edsvik said:
Hello

i've selected data from a table and put it into a dataset, now how do i
populate a combobox the easiest way?

OleDbDataAdapter DA = new OleDbDataAdapter();

OleDbCommand SQL = new OleDbCommand("SELECT * FROM Products", conn);

DataSet DS = new DataSet();

DA.SelectCommand = SQL;


DA.Fill(DS,"Products");

comboBox1.DataSource = DS; // no no no nooooooooooo :)
 
S

Steve Lutz

Hi Lasse,

I'm sorry, I thought you were using a drop drop list.. For a combo box, try

combobox1.DisplayMember = "ProductName";
combobox1.ValueMember = "ProductID";

Sorry

Steve


Lasse Edsvik said:
Steve,

hmm, i dont get any properties/methods DataTextField, DataValueField and
DataBind

only get up Datasource after hitting . after comboBox1

/Lasse


Steve Lutz said:
Lasse

You have it almost correct.

[ Database stuff here]

Combobox1.Datasource = DS.Tables["Products"];
Combobox1.DataTextField = "ProductName"; // I am guessing at column
name
Combobox1.DataValueField = "ProductID"; // Same here
Combobox1.DataBind();



Lasse Edsvik said:
Hello

i've selected data from a table and put it into a dataset, now how do i
populate a combobox the easiest way?

OleDbDataAdapter DA = new OleDbDataAdapter();

OleDbCommand SQL = new OleDbCommand("SELECT * FROM Products", conn);

DataSet DS = new DataSet();

DA.SelectCommand = SQL;


DA.Fill(DS,"Products");

comboBox1.DataSource = DS; // no no no nooooooooooo :)
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi Lasse

You are working on a win app no?
Steve's suggestion are good for the web DropDownList not for win forms's
Combobox.
In Winform's Combobox you have a similar set of properties, named
ValueMember and DisplayMember, take a look at Combobox.ValueMember in the
MSDN it gives you an example of how to use it.


Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

Lasse Edsvik said:
Steve,

hmm, i dont get any properties/methods DataTextField, DataValueField and
DataBind

only get up Datasource after hitting . after comboBox1

/Lasse


Steve Lutz said:
Lasse

You have it almost correct.

[ Database stuff here]

Combobox1.Datasource = DS.Tables["Products"];
Combobox1.DataTextField = "ProductName"; // I am guessing at column
name
Combobox1.DataValueField = "ProductID"; // Same here
Combobox1.DataBind();



Lasse Edsvik said:
Hello

i've selected data from a table and put it into a dataset, now how do i
populate a combobox the easiest way?

OleDbDataAdapter DA = new OleDbDataAdapter();

OleDbCommand SQL = new OleDbCommand("SELECT * FROM Products", conn);

DataSet DS = new DataSet();

DA.SelectCommand = SQL;


DA.Fill(DS,"Products");

comboBox1.DataSource = DS; // no no no nooooooooooo :)
 
N

Nicholas Paldino [.NET/C# MVP]

Lasse,

Steve was almost correct as well. The properties that you want to look
for are DisplayMember and ValueMember respectively.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)
Lasse Edsvik said:
Steve,

hmm, i dont get any properties/methods DataTextField, DataValueField and
DataBind

only get up Datasource after hitting . after comboBox1

/Lasse


Steve Lutz said:
Lasse

You have it almost correct.

[ Database stuff here]

Combobox1.Datasource = DS.Tables["Products"];
Combobox1.DataTextField = "ProductName"; // I am guessing at column
name
Combobox1.DataValueField = "ProductID"; // Same here
Combobox1.DataBind();



Lasse Edsvik said:
Hello

i've selected data from a table and put it into a dataset, now how do i
populate a combobox the easiest way?

OleDbDataAdapter DA = new OleDbDataAdapter();

OleDbCommand SQL = new OleDbCommand("SELECT * FROM Products", conn);

DataSet DS = new DataSet();

DA.SelectCommand = SQL;


DA.Fill(DS,"Products");

comboBox1.DataSource = DS; // no no no nooooooooooo :)
 
L

Lasse Edsvik

Steve,

coolness :))

works like a charm :)

when do i need to use databind and when dont?

Thanks again
/Lasse


Steve Lutz said:
Hi Lasse,

I'm sorry, I thought you were using a drop drop list.. For a combo box, try

combobox1.DisplayMember = "ProductName";
combobox1.ValueMember = "ProductID";

Sorry

Steve


Lasse Edsvik said:
Steve,

hmm, i dont get any properties/methods DataTextField, DataValueField and
DataBind

only get up Datasource after hitting . after comboBox1

/Lasse


Steve Lutz said:
Lasse

You have it almost correct.

[ Database stuff here]

Combobox1.Datasource = DS.Tables["Products"];
Combobox1.DataTextField = "ProductName"; // I am guessing at column
name
Combobox1.DataValueField = "ProductID"; // Same here
Combobox1.DataBind();



Hello

i've selected data from a table and put it into a dataset, now how
do
 

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