?
Solved

Populating Drop-Down Control

Posted on 2003-11-15
2
Medium Priority
?
968 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 750 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

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…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses
Course of the Month13 days, 17 hours left to enroll

809 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