Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

List multiple table columns in DropDownList

Status
Not open for further replies.

multiplex77

Programmer
Dec 25, 2001
302
SG
I have a DropDownList which I want to populate from a table (eg, Employees). I've set the DataSource property to DataSet3, which is in turn Filled with SqlDataAdapter3 (pulls up Employees table data).

I want the list to display 2 fields in Employees table in the format like "Lastname, Firstname" (Firstname and Lastname are 2 separate fields in Employees table). But my problem is I don't know how to list out multiple table fields (Firstname and Lastname) in the DataTextField property of DropDownList. I also don't know how to include additional text (the ", ") in the DataTextField.

I have 2 questions:
1. How do I do this?
2. Can it be done without too much coding? I'm just moving over from ASP and am hoping to do less coding with .NET.
2. Could someone please recommend me a reference site for ASP.NET objects like DropDownList. I find the MSFT one too technical...it looks like machine code to me. All I need is a simple description of each object property and how to set it.

Thanks for your help!
 
There's no native control that will allow you to do this: the drop list renders as a regular drop down list, so there's no way to nicely have multiple columns.

A few options though:

1. Third Party Control
I've heard of 3rd party controls that will display multiple columns (although I'm pretty sure they do this by rendering alot of dhtml and javascript), but then you have to pay for it.

2. Use the drop list, create long strings for items
So if you had a FirstName LastName field you could just concatenate the values and assign the string to the text property of the drop list's list item.
i.e.
Dim string as string
string = object.FirstName & " " & object.LastName
dropList.Items.add(New ListItem(string, value)

(omg, you know you've been coding too long when you can write out adding a list item from memory...)

3. DataGrid with Div tag
Although its not a list box, you could get fancy and bind your data to a datagrid. Place the grid in either a flow or grid layout panel (which renders as a div) and in the panel's style property, add "Overflow:auto;". What this will do is if the datagrid grows to be bigger than the div, scroll bars will appear (so the div never changes size). This can give you the ability of having multiple columns visible in a scrollable list.

Anyway, just some things off the top of my head.

hth

D'Arcy
 
Thanks D'Arcy, I'll give those a try. Thanks for your help!
 
this is an old thread, but i'm having a problem and maybe someone can look at my code. I want to display two fields in a drop-down list and here is my code. Can someone tell me what is wrong with it?

Code:
Sub FillInsuredDropDownList()
        Dim strMasterClaimsRecord As String = "SELECT FirstNamedInsured, ID, FirstNamedInsured +''+ ID AS FirstID FROM TblPolicyTable ORDER BY FirstNamedInsured"

        Dim objCommand As New OleDb.OleDbCommand(strMasterClaimsRecord, cnNewClaim)
        Dim objReader As OleDb.OleDbDataReader = Nothing

        cnNewClaim.Open()
        objReader = objCommand.ExecuteReader
        daNewClaim.SelectCommand() = objCommand
        ddlInsured.DataSource = objReader
        ddlInsured.DataTextField = "FirstID"
        ddlInsured.DataValueField = "ID"
        ddlInsured.DataBind()
        cnNewClaim.Close()
    End Sub
 
is this not possible? i was just curious if anyone could help me out.

thanks
 
What you are asking is diffent than the original thread. Having a value in the DataValueField and a concant. value in the DataTextField is possible. What doesn't work for you?

Hope everyone is having a great day!

Thanks - Jennifer
 
I'm basically asking how do you put two fields in the datatextfield, like the person in the original thread is asking..

thanks
 
this is the error i get on run:

Exception Details: System.InvalidOperationException: The provider could not determine the Double value. For example, the row was just created, the default for the Double column was not available, and the consumer had not yet set a new Double value.
 
Try replacing the + with & --- I am not sure what database you are using.


Hope everyone is having a great day!

Thanks - Jennifer
 
I got it to work like this (I used '&' instead of '+'), but
when the drop down list populates, the two fields are right next to each other like = FirstNamedInsured32

How can i put a space or tab in there?
 
Did you have a space in between the ' ' in your SQL Statement? I can't tell from the posting.

FirstNamedInsured & ' ' & ID

Hope everyone is having a great day!

Thanks - Jennifer
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top