Solved

c# populate dropdown list from a datasource?

Posted on 2013-01-18
13
357 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
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.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…
This is a video describing the growing solar energy use in Utah. This is a topic that greatly interests me and so I decided to produce a video about it.

929 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

17 Experts available now in Live!

Get 1:1 Help Now