Solved

c# populate dropdown list from a datasource?

Posted on 2013-01-18
13
367 Views
Last Modified: 2013-01-23
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
0
Comment
Question by:cycledude
  • 6
  • 4
  • 3
13 Comments
 
LVL 75

Expert Comment

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

Open in new window

0
 

Author Comment

by:cycledude
ID: 38792823
I get

No overload method for 'add' takes 2 arguments
0
 
LVL 29

Expert Comment

by:Paul Jackson
ID: 38792908
cboEmployees.Items.Add(new ListItem(dr["sname"] + ", " + dr["fname"], dr["id"]));
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 38792943
Is this a Windows Forms or ASP.NET application?
0
 

Author Comment

by:cycledude
ID: 38792952
the type or namespace  name 'ListItem' could not be found
0
 
LVL 29

Expert Comment

by:Paul Jackson
ID: 38793108
try :

cboEmployees.Items.Add(new Item(dr["sname"] + ", " + dr["fname"], dr["id"]));
0
 

Author Comment

by:cycledude
ID: 38793448
Error      1      Using the generic type 'System.Collections.Generic.List<T>' requires 1 type arguments
0
 

Author Comment

by:cycledude
ID: 38793544
this is a windows forms application
0
 
LVL 29

Expert Comment

by:Paul Jackson
ID: 38793629
0
 
LVL 29

Assisted Solution

by:Paul Jackson
Paul Jackson earned 250 total points
ID: 38793641
0
 
LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 250 total points
ID: 38793977
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
 

Accepted Solution

by:
cycledude earned 0 total points
ID: 38795890
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
 

Author Closing Comment

by:cycledude
ID: 38809230
Thanks again.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

792 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question