Export (0) Print
Expand All

How to: Format Numbers and Dates in a LightSwitch Application

By using the Format Pattern property, you can specify how numeric and date types appear in an application that you create in Visual Studio LightSwitch. For example, you might want to display a Decimal value as a percentage (0.12 as 12%) or display a Date that includes the day of the week (7/4/2011 as Monday, July 4, 2011).

Important note Important

The Format Pattern property applies only to screens in desktop clients for LightSwitch applications. For screens in an HTML client, you can apply formatting by writing JavaScript code in the postRender method of a screen. See Format a number or Format a Date.

Tip Tip

You'll often want to specify display formatting by using custom business types such as Money or Percent.

The Format Pattern property is available in the Data Designer for the following types:

  • Date

  • Date Time

  • Decimal

  • Double

  • Guid

  • Integer

  • Long Integer

  • Short Integer

To specify a format, you use the same notation as for the IFormattable.ToString method. For more information about formatting notation, see Reference: Number and Date Formats.

To format a number

  1. In Solution Explorer, open the shortcut menu for an entity or table, and then choose Open.

    The entity or table opens in the Data Designer.

    Note Note

    For applications that have been upgraded to Visual Studio 2012 Update 2, on the Perspective bar, choose the Server tab.

  2. In the Data Designer, choose the field that you want to format.

  3. On the menu bar, choose View, Properties Window.

  4. In the Properties window, in the Formatting section, choose the Format Pattern property, and then specify a valid format string.

    For more information about format strings, see Reference: Number and Date Formats.

To format a date

  1. In Solution Explorer, open the shortcut menu for an entity or table, and then choose Open.

    The entity or table opens in the Data Designer.

    Note Note

    For applications that have been upgraded to Visual Studio 2012 Update 2, on the Perspective bar, choose the Server tab.

  2. In the Data Designer, choose a field of type Date or Date Time that you want to format.

  3. On the menu bar, choose View, Properties Window.

  4. In the Properties window, in the Formatting section, choose the Format Pattern property, and then specify a valid format string.

    For more information about format strings, see Reference: Number and Date Formats.

  5. In Solution Explorer, open the shortcut menu for a screen that displays the entity or table.

    The screen opens in the Screen Designer.

  6. In the Screen Content Tree, choose the item that represents the date field that you want to format.

  7. In the list of formats, choose either Label or TextBox.

    Note Note

    The Date Picker, Date Time Picker, Date Time Viewer and Date Viewer controls don't respect the Format Pattern property.

Show:
© 2014 Microsoft