Presenting a List of Choices

In this lesson, you will learn how to define an entity field that enables a user to select from a list of predefined choices.

Create a Choice List

Sometimes an entity field represents data that must be restricted to a small set of valid values. For example, an Order Status field might have a fixed set of values: Pending Approval, Ready for Shipment, Shipped, and Backordered. To prevent a user from entering a status that is not valid, you can present these four values as a choice list.

To create a choice list

  1. In Solution Explorer, double-click Contacts.

    The Data Designer opens and displays the Contacts entity.

  2. In the Name column, click <Add Property>, type ContactType, and then press Enter.

  3. In the Type column, choose Integer.

  4. In the Required column, clear the check box.

  5. In the Properties window, click the Choice List link.

    The Choice List dialog box opens.

  6. In the Choice List dialog box, enter the following values, and then click OK.

    Value

    Display Name

    0

    Existing Customer

    1

    New Customer

    2

    Prospect

    The completed choice list

    When this field is displayed on a screen, it will present to the user a drop-down list that contains the three Display Name values that you entered.

Closer Look

In this procedure, you created a choice list that has three possible values. The choices are presented to the user at run time as a drop-down list that has three text values, but the data is still stored in its native data type, Integer. LightSwitch handles the conversion for you, and you do not need to write any code.

You can also create choice lists in which the value and display name are the same data type. For example, for a travel application you might want to provide a fixed list of destination cities. In this case, both the values presented to the user and the values stored in the data source would be of type String. Using a choice list prevents the user from entering a city that is not in the list.

Next Steps

In the next lesson, you will learn how to validate data entry.

Next Lesson: Adding Validation

See Also

Other Resources

How to: Define a Choice List for a Entity Field

Working with Data in LightSwitch (Guided Tour)