DisplayFormatAttribute.DataFormatString Property


The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience.

Gets or sets the display format for the field value.

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

public string DataFormatString { get; set; }

Property Value

Type: System.String

A formatting string that specifies the display format for the value of the data field. The default is an empty string (""), which indicates that no special formatting is applied to the field value.

Use the DataFormatString property to specify a custom display format for the values that are displayed in the DynamicField object. If the DataFormatString property is not set, the field's value is displayed without any special formatting. For more information, see Formatting Types in the .NET Framework.


When the DynamicField.HtmlEncode property is true, the value of the field is HTML encoded to its string representation before the formatting string is applied. For some objects, such as dates, you might want to control how the object is displayed with a formatting string. In those cases, you must set the DynamicField.HtmlEncode property to false.

By default, the formatting string is applied to the field value only when the data-bound control that contains the DynamicField object is in read-only mode. To apply the formatting string to field values when they are in edit mode, set the ApplyFormatInEditMode property to true.

The formatting string can be any literal string and usually includes a placeholder for the field's value. For example, in the formatting string "Item Value: {0}", the field's value is substituted for the {0} placeholder when the string is displayed in the DynamicField object. The remainder of the formatting string is displayed as literal text.


If the formatting string does not include a placeholder, the field's value from the data source is not included in the final display text.

The following examples show how to use the DataFormatString property to set the display format of data fields. The first example sets the display format for a currency type data field. The second example sets the display format for a date type data field.

// Display currency data field in the format $1,345.50.
public object StandardCost;
// Display date data field in the short format 11/12/08.
// Also, apply format in edit mode.
[DisplayFormat(ApplyFormatInEditMode=true, DataFormatString = "{0:d}")]
public object SellStartDate;

Universal Windows Platform
Available since 8
.NET Framework
Available since 3.5
Portable Class Library
Supported in: portable .NET platforms
Available since 3.0
Return to top