Last post Dec 21, 2011 11:36 AM by cyris|
Dec 14, 2011 01:33 AM|cyris||LINK
On most of my tables I have an employee ID.
Instead of displaying this ID on my tables (dropdownlist), can I display a concatination of 2 fields, First Name and Last Name, yet keep the employee ID as the value.
Is this possible?
Dec 14, 2011 01:37 AM|Chetan Chopade|LINK
You can do this by using following 2 ways
1.) DataBoundEvent of Dropdownlist.
protected void DropDownList1_DataBound(object sender, EventArgs e)
foreach (ListItem item in DropDownList1.Items)
item.Text = item.Text + " " + item.Value;
2.) Concatinate these two strings in select statement itself as assign it to dropdownlists Text property
Dec 14, 2011 01:39 AM|gopalanmani|LINK
use select query like,
FirstName +' ' +LastName as employeeName
from <employee table>
Dec 14, 2011 02:27 AM|cyris||LINK
Excuse me for my stupidity, however I'm still very new to Dynamic Data.
How can I modify the select query to dislay the name instead of the employeeID?
When I view the page markup I don't see a drop down list...
Thanks for the posts so far!
Dec 14, 2011 02:33 AM|Chetan Chopade|LINK
(FirstName +' '+ LastName) as Name
DropDownList1.DataSource = dataSourceName;
DropDownList1.DataTextField = "Name";
DropDownList1.DataValueField = "ID";
Dec 14, 2011 07:43 AM|valZ|LINK
Dec 14, 2011 10:02 AM|sjnaughton|LINK
Hi Cyris, yes you can you would override the ToString of the entity to get this to work generically. a good sample is on Rick Andersons blog here
Improving the FK field display: Showing two fields in Foreign Key columns
Dec 14, 2011 10:03 AM|sjnaughton|LINK
This will only work with Linq to SQL and will not work with EF.
Dec 14, 2011 11:45 AM|cyris||LINK
Thank you everyone!
I have the names being listed now. Who knew it was so easy ? :)
How do I go about sorting this list alphabetically ?
Dec 14, 2011 01:16 PM|sjnaughton|LINK
You will need to set the Sort Column using the DisplayColumn attribute parameters are
Dec 15, 2011 11:02 PM|cyris||LINK
Thank you very much! That worked perfectly!
Dec 16, 2011 05:49 PM|cyris||LINK
Another question regarding this. What if, for example, I have my employeeID as a forign key in another table that does not have the properties firstName and lastName ?
Can I still somehow create a proerpty called employeeName?
Dec 17, 2011 02:22 PM|sjnaughton|LINK
Dec 18, 2011 01:30 AM|RachellGrace|LINK
OK. that's great.
Dec 20, 2011 11:31 AM|cyris||LINK
Can you explain a bit further, I'm still very new to all of this.
Thanks so far
Dec 20, 2011 11:41 AM|sjnaughton|LINK
Hi Cyris, did you have look at Rick Andersons post
Improving the FK field display: Showing two fields in Foreign Key columns Rick explains it fully.
Dec 20, 2011 12:21 PM|cyris||LINK
I went and took a look and did what he suggested, overloading the ToString method and that works great.
But now I have a dropdown list of employeeNames with no idea how to sort again.
Before I was able to sort using the DisplayColumn Attribute, but now I can't.
Thanks again, much appreciated.
Dec 20, 2011 07:25 PM|sjnaughton|LINK
You still sort using the Display attribute but sadly you can only specify one column:
Dec 21, 2011 11:36 AM|cyris||LINK
I was able to combine suggestions (creating a property with FName + LName) and overriding ToString. Seems to work great.