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.

Note

Your computer might show different names or locations for some of the Visual Studio user interface elements in the following instructions. The Visual Studio edition that you have and the settings that you use determine these elements. For more information, see Visual Studio Settings.

To create a choice list

  1. In Solution Explorer, double-click Orders.

    The Data Designer opens and displays the Orders entity.

  2. In the Entity Designer, select the ShipVia field.

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

    The Choice List dialog box opens.

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

    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)