How to Select Multiple Rows in Grid Using LightSwitch 2012

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:

  1. Open Visual Studio 2012.
  2. Goto "File" => "New" => "Project..."
  3. Select "LightSwitch" in installed templates.
  4. Select "LightSwitch Application (Visual C#)".
  5. Enter the Name and choose the location.
  6. Click "OK".

First of all attach a data source, or you can create a new table; this is my table data:

img1.jpg

Image 1.

Now add a new screen and add EditableGrid and select a data source.

img2.jpg

Image 2.

Now hit F5 to run the application and select a grid row.

img3.jpg

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.

img4.jpg

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:

img5.jpg

Image 5.

Press the Ctrl keyboard button and select rows.



Up Next
    Ebook Download
    View all
    Learn
    View all