Export (0) Print
Expand All

DisplayFormatAttribute.DataFormatString Property

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.

NoteNote

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.

NoteNote

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.
[DisplayFormat(DataFormatString="{0:C}")]
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;

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5 SP1

.NET Framework Client Profile

Supported in: 4

Portable Class Library

Supported in: Portable Class Library
Show:
© 2015 Microsoft