How to: Create a Bound Control and Format the Displayed Data

 

With Windows Forms data binding, you can format the data displayed in a data-bound control by using the Formatting and Advanced Binding dialog box.

System_CAPS_ICON_note.jpg Note

The dialog boxes and menu commands you see might differ from those described in Help depending on your active settings or edition. To change your settings, choose Import and Export Settings on the Tools menu. For more information, see Customizing Development Settings in Visual Studio.

To bind a control and format the displayed data

  1. Connect to a data source.

    For more information, see Connecting to a Data Source.

  2. In the form, select the control, and then open the Properties window.

  3. Expand the (DataBindings) property, and then in the (Advanced) box, click the ellipsis button (VisualStudioEllipsesButton screenshot) to display the Formatting and Advanced Binding dialog box, which has a complete list of properties for that control.

  4. Select the property you want to bind, and then click the Binding arrow.

    A list of available data sources is displayed.

  5. Expand the data source you want to bind to until you find the single data element you want.

    For example, if you are binding to a column value in a dataset's table, expand the name of the dataset, and then expand the table name to display column names.

  6. Click the name of an element to bind to.

  7. In the Format type box, click the format you want to apply to the data displayed in the control.

    In every case, you can specify the value displayed in the control if the data source contains DBNull. Otherwise, the options vary slightly, depending on the format type you choose. The following table shows the format types and options.

    Format typeFormatting option
    No FormattingNo options.
    NumericSpecify number of decimal places by using Decimal places up-down control.
    CurrencySpecify number of decimal places by using Decimal places up-down control.
    Date TimeSelect how the date and time should be displayed by selecting one of the items in the Type selection box.
    ScientificSpecify number of decimal places by using Decimal places up-down control.
    CustomSpecify a custom format string using.

    For more information, see Formatting Types. Note: Custom format strings are not guaranteed to successfully round trip between the data source and bound control. Instead handle the Parse or Format event for the binding and apply custom formatting in the event-handling code.
  8. Click OK to close the Formatting and Advanced Binding dialog box and return to the Properties window.

How to: Create a Simple-Bound Control on a Windows Form
User Input Validation in Windows Forms
Windows Forms Data Binding

Show: