Kuzgun
Kuzgun

Reputation: 4737

Multiple values on datatextfield

I bind a dropdownlist to the datasource, and defined text and value columns on codebehind.

DropDownList1.DataTextField = "Product_Name"
DropDownList1.DataValueField = "Product_ID"

It works fine. But I want to assign one more column to DataTextField, Order_Date. I tried my chance with this:

DropDownList1.DataTextField = "Product_Name"+"Order_Date"
    DropDownList1.DataValueField = "Product_ID"

But it didn't work. Is it possible to show multiple values on DataTextField?

Upvotes: 4

Views: 6624

Answers (3)

Kurt Verheyen
Kurt Verheyen

Reputation: 11

I worked with an object datasource to fill the dropdownlist and I made a new property in the model

public string Datatextfield
{
    get
    {
        return Name + " - Date:  " + Date.ToShortDateString();
    }
    set { Datatextfield = value; }
}

And in aspx I use the property like so:

<asp:DropDownList ID="ddlList" DataSourceID="obj" runat="server" DataTextField="Datatextfield" DataValueField="ID">

Upvotes: 1

Kuzgun
Kuzgun

Reputation: 4737

It turns out, best way is to get value directly from database as Freak_Droid suggested and I followed his way.

Upvotes: 1

Vaibhav Parmar
Vaibhav Parmar

Reputation: 643

Yes this is possible try by the following code:

var datasource = from x in products
             select new {
                 x.Id,
                 x.Code,
                 x.Description,
                 DisplayField = String.Format("{0} ({1})", x.Code, x.Description)
             };

DropDownList1.DataSource = datasource;
DropDownList1.DataValueField = "Id";
DropDownList1.DataTextField = "DisplayField";
DropDownList1.DataBind();

Upvotes: 4

Related Questions