c# populate dropdown list from a datasource?

Hi

I am using c#2010, I have a queried a database and got some results based on the users request, the results are held in a oledbdatareader.

I need to populate a combobox with the text and values... I tried looping though the datareader as follows:

while (dr.Read())
                {
                    cboEmployees.Items.Add(dr["sname"] + ", " + dr["fname"] );
                    
                }

Open in new window


but i realised that i don't have the ID for the record in there... So I guess that goes into the 'value' part... how is this done?

Cheers

CD
cycledudeAsked:
Who is Participating?
 
cycledudeAuthor Commented:
Hey guys, thanks for the help.  You set me on the right road.  Here is what I have done:

I have a class

private class Item
        {
            public string Name;
            public int Value;
         
            public Item(string name, int value)
            {
                Name = name; Value = value;
            }
         
            public override string ToString()
            {
                return Name;
            }
        }

Open in new window


I then use it as follows to add the data to the combobox

                    string fname = (string)dr["fname"];
                    string sname = (string)dr["sname"];
                    string dept = (string)dr["department_name"];
                    int id = (int)dr["ID"];
                    string display = fname.Trim() + " " + sname.Trim() + " (" + dept.Trim() + ")";
                    cboEmployees.Items.Add(new Item(display, id));

Open in new window


Finally, to get the value selected by the user

            Item itm = (Item)cboEmployees.SelectedItem;
            MessageBox.Show(itm.Name + " " + itm.Value);

Open in new window


this code seems to be working fine, I find it hard to believe that adding a text/id to a combobox is so conveluted... it was easier back in the VB6 days... oh well.

Cheers for the help ;o)

CD
0
 
käµfm³d 👽Commented:
cboEmployees.Items.Add(dr["sname"] + ", " + dr["fname"], dr["id"]);

Open in new window

0
 
cycledudeAuthor Commented:
I get

No overload method for 'add' takes 2 arguments
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
Paul JacksonSoftware EngineerCommented:
cboEmployees.Items.Add(new ListItem(dr["sname"] + ", " + dr["fname"], dr["id"]));
0
 
käµfm³d 👽Commented:
Is this a Windows Forms or ASP.NET application?
0
 
cycledudeAuthor Commented:
the type or namespace  name 'ListItem' could not be found
0
 
Paul JacksonSoftware EngineerCommented:
try :

cboEmployees.Items.Add(new Item(dr["sname"] + ", " + dr["fname"], dr["id"]));
0
 
cycledudeAuthor Commented:
Error      1      Using the generic type 'System.Collections.Generic.List<T>' requires 1 type arguments
0
 
cycledudeAuthor Commented:
this is a windows forms application
0
 
Paul JacksonSoftware EngineerCommented:
0
 
käµfm³d 👽Commented:
You could try this approach:

while (dr.Read())
{
    cboEmployees.Items.Add(new { Text = dr["sname"] + ", " + dr["fname"], Value = dr["id"] });                    
}

cboEmployees.DisplayMember = "Text";
cboEmployees.ValueMember = "Value";

Open in new window

0
 
cycledudeAuthor Commented:
Thanks again.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.