In this article you will learn how to select more than one row in a grid using LightSwitch 2012.
Getting Started
Use the following to create the project:
- Open Visual Studio 2012.
- Goto "File" => "New" => "Project..."
- Select "LightSwitch" in installed templates.
- Select "LightSwitch Application (Visual C#)".
- Enter the Name and choose the location.
- Click "OK".
First of all attach a data source, or you can create a new table; this is my table data:
Image 1.
Now add a new screen and add EditableGrid and select a data source.
Image 2.
Now hit F5 to run the application and select a grid row.
Image 3.
You will see you are unable to select multiple rows, so let's work on how to select multiple rows.
First of all spread the write code events and click IntializeDataWorkspace event.
Image 4.
Now write the code.
Add the namespace first:
using System.Windows.Controls;
private const string _CONTROL = "grid";
private DataGrid _ItemsList = null;
private int _SelectedRowsCount = 0;
partial void EditableOrdersGrid_InitializeDataWorkspace(List<IDataService> saveChangesTo)
{
// Write your code here.
this.FindControl(_CONTROL).ControlAvailable += Orders_ControlAvailable;
}
private void Orders_ControlAvailable(object sender, ControlAvailableEventArgs e)
{
_ItemsList = e.Control as DataGrid;
//if the cast failed, just leave, there's nothing more we can do here
if (_ItemsList == null)
{
return;
}
//set the property on the grid that allows multiple selection
_ItemsList.SelectionMode = DataGridSelectionMode.Extended;
_ItemsList.SelectionChanged += new SelectionChangedEventHandler(_ItemsList_SelectionChanged);
}
private void _ItemsList_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
switch (_ItemsList == null)
{
case true:
_SelectedRowsCount = 0;
break;
case false:
_SelectedRowsCount = _ItemsList.SelectedItems.Count;
break;
}
}
Now run the application:
Image 5.
Press the Ctrl keyboard button and select rows.