populate a comboBox from DataSet

M

Marc Jennings

Hi there,

I'm new to C#, and I have a small problem.

I have created a new DataSet using the code below, and I would like to
be able to populat a drop-down list with the results of the "name"
field. Can anyone help me, please?

*************************************************

string m_Connect = "Data Source=(local);integrated
security=SSPI;Initial Catalog=database_name;";
SqlConnection myConn = new SqlConnection(m_Connect);
SqlDataAdapter da = new SqlDataAdapter();
myConn.Open();
DataSet ds1 = new DataSet();
string sQueryString1 = "";
sQueryString1 = "SELECT * FROM sysobjects where type='U'";
da.SelectCommand = new SqlCommand(sQueryString1,myConn);
da.Fill(ds1, "ImportTable");

*************************************************

Thanks,
Marc.
 
T

Tamir Khason

comboBox1.DataSource = ds1;
comboBox1.DisplayMember = "Whatever2See";
comboBox1.ValueMember = "Whatever2Have";

Then to bound the combobox to the SelectedValue property using the DataTable
in ds1:

comboBox1.DataBindings.Add("SelectedValue", this.ds1.Tables[0],
"Whatever2Have");

Hope it helps
 
M

Marc Jennings

Thanks for the quick reply...

If I understood you correctly, if I want to fill the combobox (called
tableslist) with the names of the tables, I should use the following
code....

tableslist.DataSource = ds1;
tableslist.DisplayMember = "name";
tableslist.ValueMember = "name";
tableslist.DataBindings.Add("SelectedValue", ds1.Tables[0],"name");

It is a very simnple app to export the contents of a given table to
XML, so all the data retrieval happens in the same function - hence
the lack of "this."

I have tried the code above and it doesn't populate the list, so I
have to assume I have misunderstood what you meant.
 
T

Tamir Khason

If your select return something like:
name id surname age
john 1 Dow 30
john 2 Brown 34
ann 3 Fox 21

by using this:
tableslist.DataSource = ds1;
tableslist.DisplayMember = "name";
tableslist.ValueMember = "name";
you should recieve combobox of 3 items named john,john and ann with the same
values
while you select one of items and onSelectionChange writes age in other
textbox by using this:
tableslist.DataBindings.Add("SelectedValue", ds1.Tables[0],"name");
You will see 21 while select third item

This works fine for me

--
Tamir Khason
You want dot.NET? Just ask:
"Please, www.dotnet.us "


Marc Jennings said:
Thanks for the quick reply...

If I understood you correctly, if I want to fill the combobox (called
tableslist) with the names of the tables, I should use the following
code....

tableslist.DataSource = ds1;
tableslist.DisplayMember = "name";
tableslist.ValueMember = "name";
tableslist.DataBindings.Add("SelectedValue", ds1.Tables[0],"name");

It is a very simnple app to export the contents of a given table to
XML, so all the data retrieval happens in the same function - hence
the lack of "this."

I have tried the code above and it doesn't populate the list, so I
have to assume I have misunderstood what you meant.

comboBox1.DataSource = ds1;
comboBox1.DisplayMember = "Whatever2See";
comboBox1.ValueMember = "Whatever2Have";

Then to bound the combobox to the SelectedValue property using the DataTable
in ds1:

comboBox1.DataBindings.Add("SelectedValue", this.ds1.Tables[0],
"Whatever2Have");

Hope it helps
 
M

Marc Jennings

Thanks again. I found out where it was all going wrong for me. I had
followed the instruction literally, and not thought to change the
DataSource from ds1 to ds1.Tables[0]

I can't believe I spent so long chasing my tail looking for this
simple answer!!!!

Thanks again.
Marc.

If your select return something like:
name id surname age
john 1 Dow 30
john 2 Brown 34
ann 3 Fox 21

by using this:
tableslist.DataSource = ds1;
tableslist.DisplayMember = "name";
tableslist.ValueMember = "name";
you should recieve combobox of 3 items named john,john and ann with the same
values
while you select one of items and onSelectionChange writes age in other
textbox by using this:
tableslist.DataBindings.Add("SelectedValue", ds1.Tables[0],"name");
You will see 21 while select third item

This works fine for me
 

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