Setting Value in AutoCompleteBox Using Code in LightSwitch 2012

This article shows how to set AutoCompleteBox values using code in a LightSwitch Application (Visual C#) in Visual Studio 2012.

The following is the procedure for setting AutoCompleteBox Values using code.

Step 1

Open the Solution Explorer.

solution.jpg

Step 2

In the Solution Explorer, right-click on the server and choose "Add Table".

add table.jpg

Step 3

In this way we add two tables (one is an Employee table and the other is an Item table). The tables are as in the following:

Employee Table

employee.jpg

City Table

city.jpg

Step 4

Now, in order to establish a relationship between Employee and City, click on the "Add Relationship" button.

add relationship.jpg

The Add New Relationship dialog box appears on the screen.

add new relationship.jpg

Step 5

Open the Employee Table, click on the "Write Code" button in the menu bar. A drop down list will appear and choose the Employee_Created method.

write code.jpg

Add the Following code to the code window:

partial void Employee_Created()

        {

            this.City = DataWorkspace.ApplicationData.Cities.Where((City) => City.CityName == "Abc").FirstOrDefault();

        }

The logic above is executed when an instance of a new employee is created.

Step 6

We can also apply some business logic.
The City could be set to "Meerut" for Employees with a EmpName beginning with "A".

For this purpose, open the Employee Table, select the EmpName and click on the "WriteCode" dropdown list and select the EmpName_Changed method.

write code1.jpg

Add the following code in the code window:

if (this.EmpName.StartsWith("A"))

            {

                this.City=DataWorkspace.ApplicationData.Cities.Where((City)=> City.CityName == "Abc").FirstOrDefault();

            }

Step 7

In the Solution Explorer, right-click on the Screens and choose "Add Screen".

add src.jpg

Step 8

The Add New Screen dialog box appears. Select the "New Data Screen" from the Screen Template, under screen information, choose "Employees" under screen data and provide some name to the Screen and click the "OK" button.

add new src.jpg

Step 9

The screen designer appears.

src desi.jpg

Step 10

Press F5 to run the application.

output1.jpg
 
Step 11

If you want to insert the city value then you can insert it using an Editable Data Screen under the screen data by choosing the City option.

However I have inserted the value under cities as "Abc" and "Meerut".

out.jpg

Up Next
    Ebook Download
    View all
    Learn
    View all