Connect with me
Search
Twitter Feed
Navigation
« jQuery 1.3 - Case Insensitive :contains() | Main | Visual Studio 2008 - Join the dark side *UPDATED* »
Tuesday
Feb242009

ASP.NET Dynamic Data - Display Custom Text in a Foreign Key DropDownList/ComboBox

I got asked a question recently about ASP.NET Dynamic Data and how to customize the display text of foreign key dropdownlists/comboboxes

The exact question was “I have a foreign key to a list of store locations. Each location has an address but the default drop down only shows the state. How do I get it to show: “State - Region - StoreName”

Here’s how you do it:

Create a partial class of your domain object and add a new column

[DisplayColumn("DisplayName", "DisplayName", false)]
public partial class Store
{
    [ScaffoldColumn(true)]
    public string DisplayName
    {
        get
        {
            return string.Format("{0}-{1}-{2}", State, Region, StoreName);
        }
    }
}

The key here is to:

  1. Create a new property with the exact formatting that you want (DisplayName in this example)
  2. Add the ScaffoldColumn attribute to the property and set it to true
  3. Add the DisplayColumn attribute to the class and use DisplayName

Note: if you don’t scaffold the column you will get an error like:

System.Web.DynamicData.MetaTable.get_DisplayColumn()
The display column ‘DisplayName’ specified for the table ‘Stores’ does not exist.

Reader Comments (7)

Hi Eric,

Slightly 'off topic' - I was checking out an article of yours about 'Dynamic Data' and then clicked on your 'photos' tab. I was amazed/surprised to see a Pomchi(?) and a shiver ran down my spine when I saw 'coco'. We have recently inherited a Pomchi, called, you guessed it, Coco. We're besotted with him. Pic at www.hollywoodgnats.blogspot.com

20 March 2010 | Unregistered CommenterPeter Bennett

Ah in the cold light of day yours looks like a pure-bred Pom, beautiful. Great set of photos, I feel like I know him/her already! Thought I'd imagined 'Coco' until I moused over!

20 March 2010 | Unregistered CommenterPeter Bennett

I'm trying the same, but it doesn't work.

[DisplayColumn("FullName", "FullName", false)]

[ScaffoldTable(true)]

[DisplayName("Anagrafica dipendenti")]

public partial class Anagr_Dip

{

[ScaffoldColumn(true)]

public string FullName

{

get

{

return string.Format("{0}-{1}-{2}", Cognome, Nome);

}

}

}

20 March 2010 | Unregistered CommenterArmando Paliani

Hi, n00b here, where do i put this code?

20 March 2010 | Unregistered Commenterrob

I couldn't make it work.

The display column 'DisplayDate' specified for the table does not exist.

Using .Net Framework 4.0 BETA

20 March 2010 | Unregistered CommenterFabio

I was having trouble getting this suggestion to work until I realized I had put the DisplayName property in the Metadata class instead of the base class. The compiler gets confused if you put it in the metadata because no column named DisplayName exists in the table. In my case, I'm working with an Opportunities table, and once I put the DisplayName property in the Opportunity class rather than Opportunity_Metadata, it worked fine.

Thanks!

02 August 2010 | Unregistered CommenterRobert Kroese

This doesn't seem to work, as suggested somewhere else (I can't find link).

A solution that does appear to work is at
http://blogs.msdn.com/b/rickandy/archive/2008/11/22/improving-the-fk-field-display-showing-two-fields-in-foreign-key-columns-with-ef.aspx

Example that would create a foreign key dropdownlist where the datatextfield is made up of three fields.....
The key is to overide ToString()....

public partial class Contact
{

public override string ToString()
{
return NameLast.ToString() + ", " + NameFirst.ToString() + ", " + Address1 + ", " + AddressPostCode ;
}

}

13 August 2010 | Unregistered CommenterKev

PostPost a New Comment

Enter your information below to add a new comment.

My response is on my own website »
Author Email (optional):
Author URL (optional):
Post:
 
Some HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>