Solved

Populating Drop-Down Control

Posted on 2003-11-15
2
959 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
Comment Utility
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
Comment Utility

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
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.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

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

6 Experts available now in Live!

Get 1:1 Help Now