Solved

c# populate dropdown list from a datasource?

Posted on 2013-01-18
13
355 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 74

Expert Comment

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

Open in new window

0
 

Author Comment

by:cycledude
Comment Utility
I get

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

Expert Comment

by:Paul Jackson
Comment Utility
cboEmployees.Items.Add(new ListItem(dr["sname"] + ", " + dr["fname"], dr["id"]));
0
 
LVL 74

Expert Comment

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

Author Comment

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

Expert Comment

by:Paul Jackson
Comment Utility
try :

cboEmployees.Items.Add(new Item(dr["sname"] + ", " + dr["fname"], dr["id"]));
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

Author Comment

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

Author Comment

by:cycledude
Comment Utility
this is a windows forms application
0
 
LVL 29

Expert Comment

by:Paul Jackson
Comment Utility
0
 
LVL 29

Assisted Solution

by:Paul Jackson
Paul Jackson earned 250 total points
Comment Utility
0
 
LVL 74

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 250 total points
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks again.
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Windows phone Development 11 27
send mail from app.net 8 25
LINQ question 7 20
Pass through dll 2 33
Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

762 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now