Alexander Hristov
Alexander Hristov

Reputation: 311

DropDownList needing 2 DataTextFields DataTable as DataSource

I'm having problems trying to give a DDL 2 DataTextFields.

        DataTable ServiceClonRegister = doc.getAllDataRegs(1, 999999999, "b.RegisterId=8 AND b.ACTIVE=1");
        iServiceClon.DataSource = ServiceClonRegister;
        iServiceClon.DataTextField = "Name" + "CODE";
        iServiceClon.DataValueField = "ROWID";
        iServiceClon.DataBind();
        iServiceClon.Items.Insert(0, new ListItem("Please, pick!", "0", true));
        iServiceClon.SelectedIndex = 0;

Any advice on how I can achieve it? Here's the html for the element:

    <select class="DropDownListHint mandatory" id="iServiceClon" runat="server" aria-describedby="lbiOffice"
                                data-live-search="true" data-placement="top" data-toggle="dropdown" data-style="DropDownListHint-datastyle">
                            </select>

Upvotes: 0

Views: 44

Answers (2)

Abdul Rehman Sayed
Abdul Rehman Sayed

Reputation: 6672

Solution 1: You can create an expression column & Add it to your DataTable.

 DataColumn dc = new DataColumn("NEwColumnName");
            dc.Expression = "Isnull(Name,'') +Isnull(Code,'')";
iServiceClon.Columns.Add(dc);

& use the new column in your dataTextfield. This will save you lots of loop.

Solution 2 : If the datatable is coming from an SQL query, create a virtual computed column there while selecting.

Upvotes: 2

Nino
Nino

Reputation: 7105

you can change code that fills DataTable in a way that query returns third column with both values. Alternatively, you can add column by yourself and put concatenated values in in. Something like this:

DataTable ServiceClonRegister = doc.getAllDataRegs(1, 999999999, "b.RegisterId=8 AND b.ACTIVE=1");
ServiceClonRegister.Columns.Add("NameAndCode");
foreach (DataRow row in ServiceClonRegister.Rows)
{
    row["NameAndCode"] = row["Name"].ToString() + row["CODE"].ToString();
}
iServiceClon.DataSource = ServiceClonRegister;
iServiceClon.DataTextField = "NameAndCode";
iServiceClon.DataValueField = "ROWID";
//rest of your code

Upvotes: 1

Related Questions