populating a combo box

  • Thread starter Thread starter Guest
  • Start date Start date
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
 
Try something like this:

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

Hope this helps...
 
Back
Top