Version

Row Selection

This topic will walk you through the process of implementing row selection on the XamDataGrid control.

Required Background

The following topics are prerequisites to understanding this topic:

Topic Purpose

This topic provides basic steps required for adding the XamDataGrid control to your view and populating it with sample data.

This resource topic provides implementation of sample data that you can use a data source for the XamDataGrid control.

Overview

The XamDataGrid control allows three modes of selection for its rows, all of which are implemented by using the SelectionMode property, exposed by the XamDataGrid control:

  • Single Row - select single row

  • Multiple Row - select multiple rows

  • None - prevents selection of rows

Single Row Selection:

  1. Set selection to single row, as demonstrated in the following code snippet.

In XAML:

<ig:XamDataGrid x:Name="DataGrid" SelectionMode="SingleRow" />

In C#:

DataGrid.SelectionMode = GridSelectionMode.SingleRow;
  1. Verify the result; the XamDataGrid control should now respond to touch input upon any of its rows by applying a selected highlighting, upon selecting a different row via touch input, the selection highlighting will be applied to that row; only the currently selected row will be displayed with an active appearance.

The following screenshot demonstrates a grid with a single row selected.

GridSelectionMode Final 1.png

Multiple Row Selection:

  1. Set selection to multiple row, as demonstrated in the following code snippet.

In XAML:

<ig:XamDataGrid x:Name="DataGrid" SelectionMode="MultipleRow" />

In C#:

DataGrid.SelectionMode = GridSelectionMode.MultipleRow;
  1. Verify the result; the XamDataGrid control will now respond to touch input upon any of its rows by applying a selected highlighting to any number of its rows; highlighting for selected rows will only be removed if those rows are selected once more via touch input.

The following screenshot illustrates this behavior, individual row selection is now independent of previously selected rows and multiple rows may not have their selected appearance applied simultaneously.

GridSelectionMode Final 2.png

Working with Selected Rows

the XamDataGrid control allows you to obtain a reference to the underlying data source objects associated with its selected rows in the following ways:

Member Purpose

Returns a collection of the selected items.

Returns a collection of the keys of selected items

Both the SelectedItems and SelectedKeys may be implemented with single and multiple row selection modes as they resolve to the entire collection of items or their keys associated with those rows that are selected.

  1. Enable row selection with the following code.

In C#:

DataGrid.SelectionMode = GridSelectionMode.MultipleRow;
  1. Add the following event handler and then iterate through the selected items to log information about these items.

In C#:

DataGrid.SelectedItemsChanged += (s, e) =>
{
    Debug.WriteLine("SelectedItems:");
    for (int i = 0; i < DataGrid.SelectedItems.Count; i++)
    {
        var item = (SampleSalesPerson)DataGrid.SelectedItems[i];
        Debug.WriteLine("index=" + i + " name=" + item.FirstName + " " + item.LastName);
    }
};
  1. Verify result: The XamDataGrid control will respond to touch input to any of its rows and the first and last name associated with the underlying item of each selected row will be logged in the log console. The following screenshot and console output illustrate this behavior.

SelectedItems:
index=0 name=William Vargas
index=1 name=Oscar Perry
index=2 name=William Perez
index=3 name=Ralph Holmes
index=4 name=Jack Adams
GridSelectionMode Final 3.png

Selecting Rows by Items

The XamDataGrid control allows the selection of its rows programmatically, by manipulating its SelectedItems collection, by the use of its SelectedItems property. Using this approach, selection of the row is accomplished by adding the underlying item that is associated with that row to the collection.

The following example demonstrates how to programmatically select a row using the SelectedItems property.

  1. Enable row selection with the following code.

In C#:

DataGrid.SelectionMode = GridSelectionMode.MultipleRow;
  1. Declare two objects by accessing their index position within the ActualDataSource of the XamDataGrid control.

In C#:

var item1 = DataGrid.ActualDataSource.GetItemAtIndex(1);
var item2 = DataGrid.ActualDataSource.GetItemAtIndex(2);
  1. Add above item object using the Add() method on the SelectedItems collection.

The parameter includes the actual item reference, whose associated row will be selected within the XamDataGrid control.

In C#:

DataGrid.SelectedItems.Add(item1);
DataGrid.SelectedItems.Add(item2);
  1. Verify result: the XamDataGrid control will programmatically select row with index 1 and 2 by looking up items in the SelectedItems collection.

GridSelectionMode Final 4.png

Selecting Rows via Keys of Items

The XamDataGrid control also allows the selection of its rows programmatically by manipulating its SelectedKeys collection, using the SelectedKeys property. Using this approach, selection of the row is accomplished by adding the key of the underlying item that is associated with a row, to the collection; this is particularly useful when working with a virtual data source, where you may not have access to the instance(s) of the item(s) that you need to be programmatically selected.

The following example demonstrates how to programmatically select a row, by the use of the SelectedKeys property.

  1. Enable row selection with the following code.

In C#:

DataGrid.SelectionMode = GridSelectionMode.MultipleRow;
  1. Create a string array instance with a size of 2, as in the following code snippet.

Here, you are targeting an item using two of its underlying public properties, in this case they are FirstName and LastName. For the first index of the array, assign the string value of FirstName and for the second index, assign the value of LastName.

In C#:

var propertyArray = new string[2];
propertyArray[0] = "FirstName";
propertyArray[1] = "LastName";
  1. Assign propertyArray as the PrimaryKey property of the XamDataGrid control.

In C#:

DataGrid.PrimaryKey = propertyArray;
Note
Note

If the primaryKey for the XamDataGrid control is not set, then the hashcode of the object will instead be used as the primaryKey; if multiple objects share the same hashcode, then all of the objects will be selected, simultaneously.

  1. Declare a new string array instance with a size of 2 and assign values of data item that you want to select.

Note
Note

For example, assign a value of “Kyle” to the first index position and assign a value of “Adams” to the second index position. Note that there must be and data item with these values otherwise the XamDataGrid control will not select any rows.

In C#:

var primaryKeyArray = new string[2];
primaryKeyArray[0] = "Kyle";
primaryKeyArray[1] = "Adams";
  1. Create a PrimaryKeyValue instance and pass in the the propertyArray, (which describes the public properties that should be considered for a matching item) and the primaryKeyArray instances, (which describes what those public property values should be, for a matching items).

In C#:

var primarykeyValue = new PrimaryKeyValue(propertyArray, primaryKeyArray);
  1. Add primaryKeyValue to the SelectedKeys collection of the XamDataGrid control.

In C#:

DataGrid.SelectedKeys.Add(primarykeyValue);
  1. Verify result: the XamDataGrid control will programmatically select the row that is associated with the data item whose FirstName property is equal to “Kyle” and whose LastName property is equal to “Adams”.

GridSelectionMode Final 5.png

Related Content

The following table lists topics that are related to this topic:

Topic Purpose

This topic provides information on supported column types in the XamDataGrid control.

This topic provides code examples on working with columns in the XamDataGrid control.