maui-405174-data-grid-selection.md
A user can tap DataGridView rows to change the selection. Three selection modes are available: no selected rows, single row, and multiple rows. You can use the control’s API to track selection changes and to specify selected rows in code.
To specify the selection behavior, set the SelectionMode property to one of the following values:
SingleA user can select a single row at a time.MultipleA user can select one or multiple rows.NoneA user cannot select rows.
To enable single row selection, set the SelectionMode property to Single.
If a user selects (taps) a row, the SelectedItem property returns the data item that corresponds to the selected row. To select a row programmatically, bind SelectedItem to a property in the View Model. Assign the required data item to that View Model property.
To clear selection, set the SelectedItem property to null.
Use the SelectedRowHandle property to obtain the selected row handle.
The following example selects a single row:
<ContentPage.BindingContext>
<local:EmployeeDataViewModel/>
</ContentPage.BindingContext>
<dx:DataGridView ...
SelectedItem="{Binding SelectedEmployee}">
<!--...-->
</dx:DataGridView>
public class EmployeeDataViewModel : INotifyPropertyChanged
{
Employee selectedEmployee;
public Employee SelectedEmployee
{
get { return selectedEmployee; }
set
{
selectedEmployee = value;
OnPropertyChanged();
}
}
public IList<Employee> Employees { get; }
public EmployeeDataViewModel()
{
SelectedEmployee = Employees[4];
}
public event PropertyChangedEventHandler? PropertyChanged;
protected void OnPropertyChanged([CallerMemberName] string name = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
}
}
To enable multiple row selection, set the SelectionMode property to Multiple.
If a user selects (taps) multiple rows, the SelectedItems property gets the collection of data items that correspond to selected rows. To select rows programmatically, bind SelectedItems to a property in the View Model. Assign required data items to the View Model’s property.
Users can tap selected rows to remove selection. To deselect all rows programmatically, set the SelectedItems property to null.
Use the GetSelectedRowHandles() method to obtain the collection of selected row handles.
The following example selects multiple rows in the data grid:
<ContentPage.BindingContext>
<local:EmployeeDataViewModel/>
</ContentPage.BindingContext>
<dx:DataGridView ...
SelectedItems="{Binding SelectedEmployees}">
<!--...-->
</dx:DataGridView>
public class EmployeeDataViewModel
{
public IList<Employee> Employees { get; }
public ObservableCollection<Employee> SelectedEmployees { get; }
public EmployeeDataViewModel()
{
SelectedEmployees = new ObservableCollection<Employee>() { Employees[0], Employees[1] };
SelectedEmployees.CollectionChanged += OnCollectionChanged;
}
private void OnCollectionChanged(object? sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
// Occurs when a user selects an item in DataGridView
}
}
The SelectionChanged event occurs if a user selects (taps) a row or if you change the SelectedItem or SelectedItems property.