Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

c# populate dropdown list from a datasource?

Posted on 2013-01-18
13
Medium Priority
?
386 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Assisted Solution

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

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 1000 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Suggested Courses

604 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