<ig:XamDataGrid x:Name="DataGrid" SelectionMode="SingleRow" />
This topic will walk you through the process of implementing row selection on the XamDataGrid control.
This topic contains the following sections
The following topics are prerequisites to understanding this topic:
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
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;
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.
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;
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.
the XamDataGrid control allows you to obtain a reference to the underlying data source objects associated with its selected rows in the following ways:
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.
Enable row selection with the following code.
In C#:
DataGrid.SelectionMode = GridSelectionMode.MultipleRow;
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);
}
};
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
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.
Enable row selection with the following code.
In C#:
DataGrid.SelectionMode = GridSelectionMode.MultipleRow;
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);
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);
Verify result: the XamDataGrid control will programmatically select row with index 1 and 2 by looking up items in the SelectedItems
collection.
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.
Enable row selection with the following code.
In C#:
DataGrid.SelectionMode = GridSelectionMode.MultipleRow;
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";
Assign propertyArray as the PrimaryKey property of the XamDataGrid control.
In C#:
DataGrid.PrimaryKey = propertyArray;
Declare a new string array instance with a size of 2 and assign values of data item that you want to select.
In C#:
var primaryKeyArray = new string[2];
primaryKeyArray[0] = "Kyle";
primaryKeyArray[1] = "Adams";
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);
Add primaryKeyValue to the SelectedKeys
collection of the XamDataGrid control.
In C#:
DataGrid.SelectedKeys.Add(primarykeyValue);
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”.
The following table lists topics that are related to this topic: