Overview of Article
The following article describes how to add a ComboBox control onto a screen for the purpose of data entry. The example project uses the customer table from the previous post. The customer table is related to a ‘Town’ table, allowing us to select a town for any given customer.
Create a table called Town with the following schema.
The Customer table from our previous post will be used for this example. In the table designer, click on the ‘Add Relationship’ button and add a ‘one to many’ relationship between Town and Customer. The schema of the table will look like the table below.
Fig1 – Adding a relationship between customer and town
Adding the relationship as illustrated above is a key step to making this work. If you are working against existing SQL Server tables outside the intrinsic ApplicationData database, it is important that relationships are properly set up at the SQL level. If relationships are not set up in SQL Server, it isn’t possible to open the screen in Fig1 to set the relationship at a LightSwitch level.
If it isn’t possible to set up relationships in SQL Server, you can use the ‘multiple datasource’ trick to get around this problem. More details can be found on this thread:
Create an ‘Editable Grid Screen’ on the town table to allow you to enter some towns.
Now moving onto the Customer data entry screen with ‘Town’ dropdown…
- Create a new screen based upon the ‘New Data Screen’ template. In the ‘Screen Data’ dropdown, choose ‘Customer’.
- By default, a ‘Two Row’ screen is created and ‘Town’ will appear as ‘Modal Window Picker’. Click the downward arrow next to Modal Window Picker to change the control to a ‘ComboBox’.
Run the project and add some towns using the Editable grid screen. Open the ‘CreateNewCustomer’ form and create a new customer. A ComboBox will appear allowing you to select a town for your new customer.