Solved

Populating Drop-Down Control

Posted on 2003-11-15
2
964 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
[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
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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

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 introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

689 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