DisplayFormatAttribute Class

Specifies how data fields are displayed and formatted.


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

[AttributeUsageAttribute(AttributeTargets.Property|AttributeTargets.Field, AllowMultiple = false)]
public class DisplayFormatAttribute : Attribute

The DisplayFormatAttribute type exposes the following members.

Public methodDisplayFormatAttributeInitializes a new instance of the DisplayFormatAttribute class.

Public propertyApplyFormatInEditModeGets or sets a value that indicates whether the formatting string that is specified in the DataFormatString property is applied to the member when in edit mode.
Public propertyConvertEmptyStringToNullGets or sets a value that indicates whether empty string values ("") are automatically converted to null.
Public propertyDataFormatStringGets or sets the string value that specifies how to display values for the member.
Public propertyNullDisplayTextGets or sets the text to display for a member when the value of the member is null.

Public methodEqualsInfrastructure. Returns a value that indicates whether this instance is equal to a specified object. (Inherited from Attribute.)
Protected methodFinalizeAllows an object to try to free resources and perform other cleanup operations before the Object is reclaimed by garbage collection. (Inherited from Object.)
Public methodGetHashCodeReturns the hash code for this instance. (Inherited from Attribute.)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Public methodMatchWhen overridden in a derived class, returns a value that indicates whether this instance equals a specified object. (Inherited from Attribute.)
Protected methodMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodToStringReturns a string that represents the current object. (Inherited from Object.)

You apply the DisplayFormatAttribute attribute to a property to specify how those property values are rendered in the user interface. You set the DataFormatString property to provide a formatting string that is appropriate for the type of data in the property. For example, if the property contains values that you want to display as scientific notation, you can set the DataFormatString property to {0:E}.

You use the ConvertEmptyStringToNull and NullDisplayText properties to specify how null and empty string ("") are rendered. You use the ApplyFormatInEditMode property to specify whether the values in the attribute are applied when the user is editing the data.

The following example shows how to apply the DisplayFormatAttribute attribute to a property named ListPrice that displays currency values and a property named Size that contains null or empty string values.

public class Product
  [DisplayFormat(DataFormatString = "{0:C}")]
  public Decimal ListPrice { get; set; }

  [DisplayFormat(ConvertEmptyStringToNull = true, NullDisplayText = "Not specified.")]
  public string Size { get; set; }


Supported in: 5, 4, 3

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

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

Community Additions