Reputation: 3201
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 back from a web service so I can't change any stored procedure to combine the columns there.
Upvotes: 31
Views: 53661
Reputation: 57
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<EmployeeDropdownInfo> 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 procedure and dropdowns only need two values most of the time.
Upvotes: -1
Reputation: 13
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";
Upvotes: 0
Reputation: 15099
You can add an additional column to the datatable that is a computed column and use it as your datatextfield (see the Microsoft Learn Multi-field Data Binding documentation page).
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"
Upvotes: 50
Reputation: 35
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)
Upvotes: 1