My previous article was about how to create and use the Composed and Scalar Queries in LightSwitch Beta 2. Today we shall discuss how to get the SelectedItem from ListView in LightSwitch Screens.
Previously we discussed Screen Layouts available in LightSwitch Beta 2. At that time I mistakenly did not discuss the ListView layout.
ListView layout is something like a GridView or Table layout in which only the first string value field will be displayed.
Let us create an application to demonstrate how to get the selected item from a ListView in LightSwitch.
Preparing the Solution:
Fireup the VS 2010; create a LightSwitch desktop application with the name "WorkingWithListViewInLSBeta2" as shown in the figure.
Follow the No: # to create the Project.
The Entity Designer:
Let us create a table.
This new screen of Beta 2 is not available in Beta 1. Select the rounded option to create a new table.
After clicking on the "Create new table" option, the Entity Designer will be displayed to create the Entity required.
To change the table name simply click on the Header of the Entity Designer and give the name. In our sample application we have given the name as Vehicle.
For this demo application, we need to create three screens. We can add a screen to the Application two ways. Here we are creating the screen from the Solution Explorer.
- Right-Click on the Screens.
- Choose the "Add Screen..." from the listed menu.
After selecting to add the screen, we will have a window for choosing the Screen Template.
As shown in the figure:
- Select the "List and Details Screen" template from the Template panel.
- Name the screen as "VehiclesListDetails"
- Select the "Vehicle" entity as the Screen data.
- Deselect the "Vehicle Details" option as we are going to work with only ListView.
Preparing the Layout:
After adding the screen we need to adjust the layout to get the screen as we expected.
Here in this above figure, delete the "Edit" and "Delete" buttons which are highlighted as we are going to add two new Buttons.
From this figure,
- Select the "Command Bar" and click on "Add" dropdownlist.
- Choose "New Button..." to create a new Button.
After clicking on "New Button..." we will have a dialog box which asks for the method name for the button.
Give it the method name "GetSelectedVehicleType" and click OK to proceed.
Now we have to write a bit of code to display the Vehicle type on click of the "Get Selected Vehicle Type".
To edit the method:
- Right-Click on the "Get Selected Vehicle Type" method.
- From the menu, Select "Edit Execute Code"
Now you will be transferred to the method.
Getting the Selected Item:
As we are using ListView, we need to get the selected item to display the type of the Vehicle selected.
Here, we are having the SelectedItem property in the Vehicles which is of type Vehicle entity which is bound with the ListView. From the selected item we can get the properties and also a method called "Delete".
The above code snippet will display the Vehicle type of the selected vehicle.
Programmatically delete the selected item:
Add another new button to the ListView with the name "DeleteSelectedItem" as shown in the figure to delete the selected item.
To delete the selected item from the ListView, edit the "DeleteSelectedItem" method as we did for the "GetSelectedVehicleType" button.
In the DeleteSelectedItem_Execute method add the code snippet as shown in the figure.
Here, we are getting the Confirmation from the user to delete the selected item as it will be removed from database itself.
To use the MessageBoxResult class to determine the option selected by the user, we need to add a reference for System.Windows.dll.
To add the namespace into our application, type "MessageBoxResult" and Press "Ctrl + dot [.]". It will show you the intelligence to select namespace to add. Just select the namespace, it will be added automatically.
Now are done.
Press F5 to see the application in action.
Application in Action:
- Add button to add the Vehicles details.
- "Get Selected Vehicle Type" is a button to get the selected vehicle type.
- "Delete Selected Item" is to delete the selected item permanently.
Now add some vehicle details to database.
When you select a Vehicle from the ListView and press the "Get Selected Vehicle Type" button, the result will be as shown in the following figure.
See the following figure which asks the user to delete the selected item permanently.
In this article, we have seen how to get the SelectedItem item and how to process the selected item in LightSwitch application.
Thanks for spending your precious time here. Please provide your valuable feedbacks and comments, which enable me to give a better article the next time.