Solved

Populating Drop-Down Control

Posted on 2003-11-15
2
961 Views
Last Modified: 2010-05-18
I have a table with two columns which I would like to drive a drop-down control. I would like the DataTextField to be the values of the two columns concatenated together with some punctuation added, and the DataValueField to be the value of the second column only.

Example:

Data: Column 1: "Attendee"
         Column 2: "150"

In the Drop-Down: DataTextField: "Attendee ($150)"
                            DateValueField: "150"

I can't seem to concatenate the columns together, either in the SQL statement itself, or by going through the data in a datareader and formatting the entries into the drop-down.

I have seen some VB samples like this:

   Loop through a datareader and for each row:

   droplist.Items.Add(New ListItem("textfield", "valuefield"))

but I can't get anything like this to work in C#.

Thanks for the help.
Ted
0
Comment
Question by:tk3
2 Comments
 
LVL 10

Accepted Solution

by:
smegghead earned 250 total points
ID: 9756953
In the SQL statement, can't you just have

select fdesc+' ($'+fval+')' as col1,fval as col2 from ...

then just use col1 and col2 as your fields ?

HTH

Smg.
0
 

Expert Comment

by:onura
ID: 9771113

Hi there,
Do you mean something like this?

// assuming that you have a list named myList containing elements type of myListItem
// and you have a drop down list named myDropDownList
foreach (myListItem li in myList)
{
  myDropDownList.Items.Add(new ListItem(li.Text,li.Value));

}

You may populate the collection to be iterated from your database query and use the dataset, or if you are using a database access layer, you may use your own types of collections.


However there is another way to populate these dropdownlists, you may directly set the datasource of the dropdownlist to a 2 column dataset that you populated.

Hope this helps,
Sincerely,

Onur

www.onura.net



0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

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…
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 Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

776 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