locked
Display Name instead of Primary Key RRS feed

  • Question

  • User-1959649351 posted

    Hey everyone,

    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?

    Thanks!

    Wednesday, December 14, 2011 1:33 AM

Answers

  • User350138131 posted

    [MetadataType(typeof(Employee_MD))]

    public partial class Employee

    {

        public string EmployeeName

        {

           get

            {

                return this.FirstName+" "+this.LastName;

            }

        }

    }

    [DisplayName("Employees")]

    [DisplayColumn("EmployeeName")]

    public class Employee_MD

    {

        ....

    }

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, December 14, 2011 7:43 AM
  • User-330204900 posted

    You will need to set the Sort Column using the DisplayColumn attribute parameters are

    1. Column to display (you have overridden that using the ToString I assume)
    2. Column to Sort on
    3. Sort Order true = descending, false = Ascending
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, December 14, 2011 1:16 PM
  • User-330204900 posted

    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.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, December 20, 2011 11:41 AM
  • User-330204900 posted

    You still sort using the Display attribute but sadly you can only specify one column:

    [Display("DisplayColumn","SortColumn",true)]

    parameters:

    1. The name of the column to display - does nothing if ToString is overriden
    2. The column to sort the dropdown list by (sorry only one column can be specified)
    3. if set to true changes sort order to descending (optional)
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, December 20, 2011 7:25 PM

All replies

  • User-362017084 posted

    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

    Wednesday, December 14, 2011 1:37 AM
  • User960609757 posted

    hi,

    use select query like,

    Select
    employeeID,
    FirstName +' ' +LastName as employeeName
    from <employee table>

    Wednesday, December 14, 2011 1:39 AM
  • User-1959649351 posted

    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!

    Wednesday, December 14, 2011 2:27 AM
  • User-362017084 posted
    Select 
                ID,
                (FirstName +'  '+ LastName) as Name
    from 
                tableName
     
    DropDownList1.DataSource = dataSourceName;
                DropDownList1.DataTextField = "Name";
                DropDownList1.DataValueField = "ID";
                DropDownList1.DataBind();
     
    Wednesday, December 14, 2011 2:33 AM
  • User350138131 posted

    [MetadataType(typeof(Employee_MD))]

    public partial class Employee

    {

        public string EmployeeName

        {

           get

            {

                return this.FirstName+" "+this.LastName;

            }

        }

    }

    [DisplayName("Employees")]

    [DisplayColumn("EmployeeName")]

    public class Employee_MD

    {

        ....

    }

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, December 14, 2011 7:43 AM
  • User-330204900 posted

    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

    Wednesday, December 14, 2011 10:02 AM
  • User-330204900 posted

    [MetadataType(typeof(Employee_MD))]

    public partial class Employee

    {

        public string EmployeeName

        {

           get

            {

                return this.FirstName+" "+this.LastName;

            }

        }

    }

    [DisplayName("Employees")]

    [DisplayColumn("EmployeeName")]

    public class Employee_MD

    {

        ....

    }

     

    This will only work with Linq to SQL and will not work with EF.

    Wednesday, December 14, 2011 10:03 AM
  • User-1959649351 posted

    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 ?

    Wednesday, December 14, 2011 11:45 AM
  • User-330204900 posted

    You will need to set the Sort Column using the DisplayColumn attribute parameters are

    1. Column to display (you have overridden that using the ToString I assume)
    2. Column to Sort on
    3. Sort Order true = descending, false = Ascending
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, December 14, 2011 1:16 PM
  • User-1959649351 posted

    Thank you very much! That worked perfectly!

    Thursday, December 15, 2011 11:02 PM
  • User-1959649351 posted

    Hey everyone,

    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?

    Thanks

    Friday, December 16, 2011 5:49 PM
  • User-330204900 posted
    <p>Hi Cyris, you apply this to the entity it's self so you will not have na issues with similar FK names :)</p>
    Saturday, December 17, 2011 2:22 PM
  • User-1148031128 posted

    OK. that's great.

    Sunday, December 18, 2011 1:30 AM
  • User-1959649351 posted

    Hey Steve,

    Can you explain a bit further, I'm still very new to all of this.

    Thanks so far

    Tuesday, December 20, 2011 11:31 AM
  • User-330204900 posted

    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.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, December 20, 2011 11:41 AM
  • User-1959649351 posted

    Hey Steve,

    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.

    Any ideas?

    Thanks again, much appreciated.

    Tuesday, December 20, 2011 12:21 PM
  • User-330204900 posted

    You still sort using the Display attribute but sadly you can only specify one column:

    [Display("DisplayColumn","SortColumn",true)]

    parameters:

    1. The name of the column to display - does nothing if ToString is overriden
    2. The column to sort the dropdown list by (sorry only one column can be specified)
    3. if set to true changes sort order to descending (optional)
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, December 20, 2011 7:25 PM
  • User-1959649351 posted

    Hey everyone,

    I was able to combine suggestions (creating a property with FName + LName) and overriding ToString. Seems to work great.

    Wednesday, December 21, 2011 11:36 AM