Theomax
Theomax

Reputation: 6800

How to set drop down list DataTextField to display two data property fields?

I have a drop down list that is populated with data in the code behind. The DataTextField attribute is set with the following code:

ddlItems.DataTextField = "ItemName";

I want to display two data properties as the DataTextField. I tried using the following code but it didn't work. And I have researched on the net but couldn't find how to use two data properties.

ddlItems.DataTextField = "ItemName" + "ItemDescription";

What code do I need to use to do this?

Upvotes: 20

Views: 104404

Answers (12)

Imtiyaz Ahmad
Imtiyaz Ahmad

Reputation: 1

In C# we can bind multiple columns to a datatextfield by creating a property which is concatenation of all requisite columns like in case of Full Name

public string FullName
{
            get
            {
                return  RollNo + Name +  Parentage;
            }
}

After creating FullName property use datatextfeild="FullName"

Upvotes: 0

AAnchal Singh
AAnchal Singh

Reputation: 1

objDT.Columns.Add("FullName", GetType(String), "time + ' ' + location")
                        objDT.AcceptChanges()
                        ddlPickUp.DataSource = objDT
                        ddlPickUp.DataTextField = "FullName"
                        ddlPickUp.DataValueField = "location"
                        ddlPickUp.DataBind()

Upvotes: 0

Tommaso
Tommaso

Reputation: 351

just fill in the dropdownlist from code behind

HTML

<asp:DropDownList ID="ddlSample" runat="server"></asp:DropDownList>

VB.NET

        Dim Conn As SqlConnection
        Dim Comm As SqlCommand
        Dim Read As SqlDataReader

        Conn = New SqlConnection()
        Conn.ConnectionString = ConfigurationManager.ConnectionStrings("Read").ConnectionString

        Comm = New SqlCommand()
        Comm.CommandType = CommandType.Text
        Comm.CommandText = "SELECT * FROM TABLE"
        Comm.Connection = Conn
        Comm.Connection.Open()

        Read = Comm.ExecuteReader()
        ddlSample.Items.Insert(0, New ListItem("Seleziona", ""))
        While Read.Read()
            ddlSample.Items.Insert(1, New ListItem(Read("ID") & " - " & Read("Desc")), ReadLivelli("ID")))
        End While

        Read.Close()
        Comm.Connection.Close()
        Comm.Dispose()
        Conn.Dispose()

Upvotes: 1

Ali Mahmoodi
Ali Mahmoodi

Reputation: 1204

Simplest way is in .aspx file :

<asp:DropDownList ID="ddlItems" runat="server" DataSourceID="YourDataSource" DataTextField='<%# Eval("Id")+" - "+Eval("Title") %>' ..... />

Hope useful.

Upvotes: 3

Davezilla
Davezilla

Reputation: 71

I have done something similar to this in two ways: either in C# or SQL.

Matteo Gaggiano below gives a good example of the C# method:

dataTable.Columns.Add(new DataColumn("Title", System.Type.GetType("System.String"), "ItemName + ' - ' + ItemDescription"));

ddlItems.DataSource = dataTable;
ddlItems.DataTextField = "Title";
ddlItems.DataValueField = "Id";
ddlItems.DataBind();

Or it can be done in SQL:

SELECT ItemName + ' - ' + ItemDescription AS Title FROM yourSQLtable

and then using the last four lines above in the C# code.

Upvotes: 4

ali bencharda
ali bencharda

Reputation: 51

The following code worked for me:

select id, ItemName + ' - ' + ItemDescription as 'yournamecolumn' from `yourtable`
ddlItems.DataTextField = "yournamecolumn";
ddlItems.DataValueField = "id";
ddlItems.DataBind();

Upvotes: 0

Matteo Gaggiano
Matteo Gaggiano

Reputation: 1305

Starting from a comment on Format DropDownList.TextValue I learned about DataColumn.Expression (DataColumn.Expression Property so this may be a valid solution (In my case it was perfect):

DataColumn title = new DataColumn();
title.ColumnName = "Title";
title.DataType = System.Type.GetType("System.String");
title.Expression = "ItemName + ' - ' + ItemDescription";
dataTable.Columns.Add(title);

// Or in one line

dataTable.Columns.Add(new DataColumn("Title", System.Type.GetType("System.String"), "ItemName + ' - ' + ItemDescription"));

ddlItems.DataSource = dataTable;
ddlItems.DataTextField = "Title";
ddlItems.DataValueField = "Id";
ddlItems.DataBind();

Upvotes: 1

Syed Adil Hasan
Syed Adil Hasan

Reputation: 1

Try this:

select std.Student_ID,std.Student_Name+' - '+ std.Admission_ID Student_Name 
from Student_tbl std , School_tbl sch 
where sch.School_ID = '" + School_ID + "' and std.Current_Class = '" + Class_ID + "' 
      and std.Section_ID = '" + Section_ID + "'

DL_Student.DataTextField = "Student_Name";
DL_Student.DataValueField = "Student_ID";

Upvotes: 0

cryptic&#39;l
cryptic&#39;l

Reputation: 53

If you're coding with entity framework code-first you could create a new field called NameAndDescription and set your ddlItems.DataTextField = "NameAndDescription". The field can be created as follows:

    public String NameAndDescription
    {
        get { return this.ItemName + this.ItemDescription; }
    }

Upvotes: 0

Kris Senden
Kris Senden

Reputation: 441

You can use LinqToSql to make a new datasource containing a displayfield which is formatted, the way you want it to be, like:

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

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

Upvotes: 28

YoryeNathan
YoryeNathan

Reputation: 14522

You can use the Formatting event of ddlItems which will allow you to set logic to how to convert a certain item to a string, or if it makes any sense to use in your program, have a property like RepresentingString that returns Name + Desc, and bind to that property.

Similar question with more answers and code examples: Format DropDownList.TextValue

Upvotes: 7

user1017882
user1017882

Reputation:

I've achieved this before but I did so using telerik radcombobox (which was capable of including an item template). The template was simply a html table and I seemingly 'concatenated' the two fields. I'm pretty sure the standard asp .net control doesn't have this functionality - but if you look around you will find a 3rd party control that will.

It is of course possible to deal with this concatenation at the business layer and define a new type (struct is probably best) with a property that contains the two fields as one string.

I'm sure there are other ways of achieving this (more efficient ways no doubt), these are just some ways I know of that may help.

Upvotes: 1

Related Questions