开发者

Combining two fields in a DataTextField. Is this possible?

I have a dataset which I am binding to a listbox. However, I want to combine two of the fields to make up the DataTextField. Is this possible, or am I going to have to loop through the Data Rows?

lstAddressDropdown.DataSource = dsAddress;
lstAddressDropdown.DataTextField = "StreetAddress" + "Place";
lstAddressDropdown.DataBind();
lstAddressDropdown.Items.Insert(0, new ListItem("Please select"));

It's worth noting that the dataset comes b开发者_运维问答ack from a web service so I can't change any stored procedure to combine the columns there.


You can add an additional column to the datatable that is a computed column and use it as your datatextfield (docs: Multi-field Data Binding).

So for your example above you could do something like this:

dsAddress.Tables[0].Columns.Add("StreetAndPlace",typeof(string),"StreetAddress + Place");
lstAddressDropdown.DataSource = dsAddress;
lstAddressDropdown.DataTextField = "StreetAndPlace";
lstAddressDropdown.DataBind();
lstAddressDropdown.Items.Insert(0, new ListItem("Please select"));

To add a space between the StreetAddress and Place replace the expression string shown above with "StreetAddress + ' ' + Place"


Modify your query as per your requirement in Oracle

SELECT
'<B>'||SHORTNAME||'</B><br/>('||DEPARTMENT||')' AS USERNAME
FROM TABLE

In above query Name will show in bold and below it department will display like:

<b>Mr. Jagdeep Mankotia</b><br>
(Web Application Development)


I know this has been answered already but I think there is an easier way to solve this problem.

Modify your select command and combine the 2 DataTextField you want to display.

DataSource.SelectCommand = "Select (Column1 || ' ' || Column2) As Column1And2 From Table";

RadioButtonList1.DataTextField = "Column1And2";

Hope this helps, Maybe it's not the best way but I think it's a lot easier to understand.


You never need to write loops anymore with linq.

  • Create a class to return your two fields.

    public partial class EmployeeDropdownInfo { public int person_id { get; set; } public string employee_name { get; set; } }

  • Get your data and use linq to filter and return what you need.

    List requestors = (from t in dc.sp_GetEmployees() select new EmployeeDropdownInfo() { person_id = t.person_id, employee_name = t.first_name + " " + t.last_name }).ToList();

    EmployeeDropdownInfo firstEntry2 = new EmployeeDropdownInfo() { person_id = 0, employee_name = "-- Select an Employee -- " }; requestors.Insert(0, firstEntry2); ddlRequestor.DataSource = requestors; ddlRequestor.DataTextField = "employee_name"; ddlRequestor.DataValueField = "person_id"; ddlRequestor.DataBind();

This way you don't have to change the proc and dropdowns only need two values most of the time.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜