Export (0) Print
Expand All

DisplayColumnAttribute Class

Updated: July 2008

Specifies the column that is displayed in the referred table as a foreign-key column.

Namespace:  System.ComponentModel.DataAnnotations
Assembly:  System.ComponentModel.DataAnnotations (in System.ComponentModel.DataAnnotations.dll)

[AttributeUsageAttribute(AttributeTargets.Class, Inherited = true, AllowMultiple = false)]
public class DisplayColumnAttribute : Attribute

When a column in a table contains a foreign key, Dynamic Data infers the display value for that column from the referenced table. By default, the first column of the referenced table whose type is string (or text) is used. The DisplayColumnAttribute attribute enables you to change this default behavior and to specify a different parent table column to use as the column for the foreign key field.

When you apply this attribute, you must adhere to the following usage constraints:

  • The attribute can only be applied to a class.

  • The attribute can be inherited by derived classes.

  • The attribute can be applied only one time.

The following example shows how to use the DisplayColumnAttribute attribute. The example performs the following tasks:

  • Implements metadata partial classes for the Address and Customer tables, which are parent tables for the CustomerAddress table.

  • Applies the DisplayColumnAttribute attribute to the Address metadata partial class to specify the following:

    • The City column from the Address table (the parent table) is displayed as the foreign-key column in the CustomerAddress child table.

    • The PostalCode column from the Address table (the parent table) is used for sorting the Address selection box in the CustomerAddress child table.

    • The sort order is set to ascending.

The example applies the DisplayColumnAttribute attribute to the Customer metadata partial class to specify that the LastName column in displayed as the foreign-key column for the CustomerAddress table.

using System;
using System.Web.DynamicData;
using System.ComponentModel.DataAnnotations;
using System.Globalization;

[DisplayColumn("City", "PostalCode", false)]
public partial class Address
{

}


[DisplayColumn("LastName")]
public partial class Customer
{


}

To compile the example, you need the following:

  • Visual Studio 2008 Service Pack 1 or Visual Developer 2008 Express Edition Service Pack 1.

  • The AdventureWorksLT sample database. For information about how to download and install the SQL Server sample database, see Microsoft SQL Server Product Samples: Database on the CodePlex site. Make sure that you install the correct version of the sample database for the version of SQL Server that you are running (SQL Server 2005 or SQL Server 2008).

  • A dynamic data-driven Web site. This enables you to create a data context for the database and to create the class that contains the data field to customize. For more information, see Walkthrough: Creating a New Dynamic Data Web Site using Scaffolding.

System.Object
  System.Attribute
    System.ComponentModel.DataAnnotations.DisplayColumnAttribute

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 7, Windows Vista, Windows XP SP2, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5 SP1

Date

History

Reason

July 2008

Added topic for new class.

SP1 feature change.

Community Additions

ADD
Show:
© 2014 Microsoft