populating a combo box

G

Guest

Hello,
I'm new to C# and I can't seem to manage doing something which is supposed
to be very simple:
I have a table in SQL Server with 2 columns: Id, Name.
I have a form in visual studio 2005 in which there is a combo box. I want to
fill this combo box manually (not using binding) with these Ids and names (I
want the name to be displayed on screen, but the actual value to be the id).
So I got a DataTable containing all the rows from the table, and I'm trying
to fill to combo box.
I've noticed I can write: ComboBox.Items.Add(row["id"]) but then the Id is
the value and the text.
How do I do the same that binding does? How do I make each Item in the combo
box to represent the Id and the name at the same time (the id as its value
and the name as its text)?

thanks
 
P

Phil Townsend

Try something like this:

foreach(DataRow dr in table.Rows)
{
ComboBox.Items.Add(new
ListItem(row["id"].ToString(),row["Name"].ToString()));
}

Hope this helps...
 

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