DataControlBase.SelectionMode Property
Gets or sets whether multiple row/cell selection is enabled. This is a dependency property.
Namespace: DevExpress.Xpf.Grid
Assembly: DevExpress.Xpf.Grid.v19.1.Core.dll
Declaration
public MultiSelectMode SelectionMode { get; set; }
Public Property SelectionMode As MultiSelectMode
Property Value
Available values:
Name |
Description |
None
|
Multi-selection is disabled.
|
Row
|
Allows selection of multiple rows.
|
Cell
|
Allows selection of multiple cells.
|
MultipleRow
|
Allows toggling the selection of multiple rows.
|
Multiple row/cell selection is not allowed if the DataViewBase.NavigationStyle property is set to GridViewNavigationStyle.None and/or the SelectionMode property is set to MultiSelectMode.None.
To enable multiple row selection, set the SelectionMode property to MultiSelectMode.Row.
To enable touch-friendly multiple row selection, set the SelectionMode property to MultiSelectMode.MultipleRow.
To enable multiple cell selection (block selection), set the SelectionMode property to MultiSelectMode.Cell.
To enable single row selection, set the DataViewBase.NavigationStyle property to GridViewNavigationStyle.Row and/or the SelectionMode property to MultiSelectMode.None.
For the CardView, setting the SelectionMode property affects card selection (similar to row selection in the TableView).
To disable card selection, set the SelectionMode property to MultiSelectMode.None or MultiSelectMode.Cell.
Example: How to Select Rows that Contain the Specified Value
This example shows how to select rows whose UnitPrice column contains a value equal to or greater than 20. To select rows, click the Select button.
<Window x:Class="DXGrid_SelectRows.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:dxg="http://schemas.devexpress.com/winfx/2008/xaml/grid"
Title="Window1" Height="293" Width="494">
<StackPanel>
<dxg:GridControl Name="grid" AutoGenerateColumns="AddNew" SelectionMode="Row" Height="220">
<dxg:GridControl.View>
<dxg:TableView Name="tableView" AutoWidth="True" />
</dxg:GridControl.View>
</dxg:GridControl>
<Button Content="Select" Width="50" Height="30" Margin="5"
HorizontalAlignment="Left" Click="Button_Click" />
</StackPanel>
</Window>
using System;
using System.Collections.Generic;
public class Product {
public string CompanyName { get; set; }
public string Country { get; set; }
public string City { get; set; }
public double UnitPrice { get; set; }
public int Quantity { get; set; }
}
public class ProductList {
static public List<Product> GetData() {
List<Product> list = new List<Product>();
list.Add(new Product() { CompanyName = "Island Trading",
Country = "UK", City = "Cowes", UnitPrice = 19, Quantity = 10 });
list.Add(new Product() { CompanyName = "Reggiani Caseifici",
Country = "Italy", City = "Reggio Emilia", UnitPrice = 12.5, Quantity = 16 });
list.Add(new Product() { CompanyName = "Ricardo Adocicados",
Country = "Brazil", City = "Rio de Janeiro", UnitPrice = 19, Quantity = 12 });
list.Add(new Product() { CompanyName = "QUICK-Stop",
Country = "Germany", City = "QUICK-Stop", UnitPrice = 22, Quantity = 50 });
list.Add(new Product() { CompanyName = "Split Rail Beer & Ale",
Country = "USA", City = "Reggio Emilia", UnitPrice = 18, Quantity = 20 });
list.Add(new Product() { CompanyName = "Ernst Handel",
Country = "Austria", City = "Graz", UnitPrice = 21, Quantity = 52 });
list.Add(new Product() { CompanyName = "Save-a-lot Markets",
Country = "USA", City = "Boise", UnitPrice = 7.75, Quantity = 120 });
list.Add(new Product() { CompanyName = "Tortuga Restaurante",
Country = "Mexico", City = "México D.F.", UnitPrice = 21, Quantity = 15 });
list.Add(new Product() { CompanyName = "Bottom-Dollar Markets",
Country = "Canada", City = "Tsawwassen", UnitPrice = 44, Quantity = 16 });
return list;
}
}
using System.Windows;
namespace DXGrid_SelectRows {
public partial class Window1 : Window {
public Window1() {
InitializeComponent();
grid.ItemsSource = ProductList.GetData();
}
private void Button_Click(object sender, RoutedEventArgs e) {
SelectProducts(20);
}
private void SelectProducts(double minPrice) {
grid.BeginSelection();
grid.UnselectAll();
for (int i = 0; i < grid.VisibleRowCount; i++) {
int rowHandle = grid.GetRowHandleByVisibleIndex(i);
if (!grid.IsGroupRowHandle(rowHandle)) {
double price = (double)grid.GetCellValue(rowHandle,
grid.Columns["UnitPrice"]);
if (price >= minPrice)
grid.SelectItem(rowHandle);
}
}
grid.EndSelection();
}
}
}
Imports Microsoft.VisualBasic
Imports System
Imports System.Collections.Generic
Public Class Product
Private privateCompanyName As String
Public Property CompanyName() As String
Get
Return privateCompanyName
End Get
Set(ByVal value As String)
privateCompanyName = value
End Set
End Property
Private privateCountry As String
Public Property Country() As String
Get
Return privateCountry
End Get
Set(ByVal value As String)
privateCountry = value
End Set
End Property
Private privateCity As String
Public Property City() As String
Get
Return privateCity
End Get
Set(ByVal value As String)
privateCity = value
End Set
End Property
Private privateUnitPrice As Double
Public Property UnitPrice() As Double
Get
Return privateUnitPrice
End Get
Set(ByVal value As Double)
privateUnitPrice = value
End Set
End Property
Private privateQuantity As Integer
Public Property Quantity() As Integer
Get
Return privateQuantity
End Get
Set(ByVal value As Integer)
privateQuantity = value
End Set
End Property
End Class
Public Class ProductList
Public Shared Function GetData() As List(Of Product)
Dim list As New List(Of Product)()
list.Add(New Product() With {.CompanyName = "Island Trading", _
.Country = "UK", .City = "Cowes", .UnitPrice = 19, .Quantity = 10})
list.Add(New Product() With {.CompanyName = "Reggiani Caseifici", _
.Country = "Italy", .City = "Reggio Emilia", .UnitPrice = 12.5, .Quantity = 16})
list.Add(New Product() With {.CompanyName = "Ricardo Adocicados", _
.Country = "Brazil", .City = "Rio de Janeiro", .UnitPrice = 19, .Quantity = 12})
list.Add(New Product() With {.CompanyName = "QUICK-Stop", _
.Country = "Germany", .City = "QUICK-Stop", .UnitPrice = 22, .Quantity = 50})
list.Add(New Product() With {.CompanyName = "Split Rail Beer & Ale", _
.Country = "USA", .City = "Reggio Emilia", .UnitPrice = 18, .Quantity = 20})
list.Add(New Product() With {.CompanyName = "Ernst Handel", _
.Country = "Austria", .City = "Graz", .UnitPrice = 21, .Quantity = 52})
list.Add(New Product() With {.CompanyName = "Save-a-lot Markets", _
.Country = "USA", .City = "Boise", .UnitPrice = 7.75, .Quantity = 120})
list.Add(New Product() With {.CompanyName = "Tortuga Restaurante", _
.Country = "Mexico", .City = "México D.F.", .UnitPrice = 21, .Quantity = 15})
list.Add(New Product() With {.CompanyName = "Bottom-Dollar Markets", _
.Country = "Canada", .City = "Tsawwassen", .UnitPrice = 44, .Quantity = 16})
Return list
End Function
End Class
Imports Microsoft.VisualBasic
Imports System.Windows
Namespace DXGrid_SelectRows
Partial Public Class Window1
Inherits Window
Public Sub New()
InitializeComponent()
grid.ItemsSource = ProductList.GetData()
End Sub
Private Sub Button_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
SelectProducts(20)
End Sub
Private Sub SelectProducts(ByVal minPrice As Double)
grid.BeginSelection()
grid.UnselectAll()
For i As Integer = 0 To grid.VisibleRowCount - 1
Dim rowHandle As Integer = grid.GetRowHandleByVisibleIndex(i)
If (Not grid.IsGroupRowHandle(rowHandle)) Then
Dim price As Double = CDbl(grid.GetCellValue( _
rowHandle, grid.Columns("UnitPrice")))
If price >= minPrice Then
grid.SelectItem(rowHandle)
End If
End If
Next i
grid.EndSelection()
End Sub
End Class
End Namespace
Example: How to Change the Appearance of Selected Cells
You should implement your own CellStyle. In this CellStyle, implement style triggers for the SelectionState property. Four values can be assigned to this property: None, Focused, Selected, FocusedAndSelected.
<Window x:Class="DXGrid_ChangeRowAppearance.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:dxg="http://schemas.devexpress.com/winfx/2008/xaml/grid"
Title="Window1" Height="300" Width="505"
xmlns:dxgt="http://schemas.devexpress.com/winfx/2008/xaml/grid/themekeys">
<Window.Resources>
<Style x:Key="SelectionStateCellStyle" TargetType="dxg:LightweightCellEditor"
BasedOn="{StaticResource ResourceKey={dxgt:GridRowThemeKey ResourceKey=LightweightCellStyle}}">
<Style.Triggers>
<Trigger Property="SelectionState" Value="Selected">
<Setter Property="Background" Value="Red"/>
</Trigger>
<Trigger Property="SelectionState" Value="Focused">
<Setter Property="Background" Value="Green"/>
<Setter Property="TextBlock.Foreground" Value="Pink"/>
</Trigger>
<Trigger Property="SelectionState" Value="FocusedAndSelected">
<Setter Property="Background" Value="Gray"/>
<Setter Property="TextBlock.Foreground" Value="Black"/>
</Trigger>
</Style.Triggers>
</Style>
<Style x:Key="FocusedRowStyle" TargetType="dxg:RowControl">
<Style.Triggers>
<Trigger Property="dxg:GridViewBase.IsFocusedRow" Value="True">
<Setter Property="Background" Value="Gray" />
<Setter Property="Foreground" Value="White" />
</Trigger>
</Style.Triggers>
</Style>
</Window.Resources>
<Grid>
<dxg:GridControl x:Name="grid" AutoGenerateColumns="AddNew" SelectionMode="Cell">
<dxg:GridControl.View>
<dxg:TableView AutoWidth="True"
CellStyle="{StaticResource SelectionStateCellStyle}"
ShowGroupPanel="False" AllowGrouping="False"
UseLightweightTemplates="All">
</dxg:TableView>
</dxg:GridControl.View>
</dxg:GridControl>
</Grid>
</Window>
using System.Windows;
using System.Collections.Generic;
namespace DXGrid_ChangeRowAppearance {
public partial class Window1 : Window {
public Window1() {
InitializeComponent();
grid.ItemsSource = Products.GetData();
}
public class Products {
public static List<Product> GetData() {
List<Product> data = new List<Product>();
data.Add(new Product() { ProductName = "Chai",
UnitPrice = 18, UnitsOnOrder = 10 });
data.Add(new Product() { ProductName = "Ipoh Coffee",
UnitPrice = 36.8, UnitsOnOrder = 12 });
data.Add(new Product() { ProductName = "Outback Lager",
UnitPrice = 12, UnitsOnOrder = 25 });
data.Add(new Product() { ProductName = "Boston Crab Meat",
UnitPrice = 18.4, UnitsOnOrder = 18 });
data.Add(new Product() { ProductName = "Konbu",
UnitPrice = 6, UnitsOnOrder = 24 });
return data;
}
}
public class Product {
public string ProductName { get; set; }
public double UnitPrice { get; set; }
public int UnitsOnOrder { get; set; }
}
}
}
Imports Microsoft.VisualBasic
Imports System.Windows
Imports System.Collections.Generic
Namespace DXGrid_ChangeRowAppearance
Partial Public Class Window1
Inherits Window
Public Sub New()
InitializeComponent()
grid.ItemsSource = Products.GetData()
End Sub
Public Class Products
Public Shared Function GetData() As List(Of Product)
Dim data As New List(Of Product)()
data.Add(New Product() With {.ProductName = "Chai", .UnitPrice = 18, .UnitsOnOrder = 10})
data.Add(New Product() With {.ProductName = "Ipoh Coffee", .UnitPrice = 36.8, .UnitsOnOrder = 12})
data.Add(New Product() With {.ProductName = "Outback Lager", .UnitPrice = 12, .UnitsOnOrder = 25})
data.Add(New Product() With {.ProductName = "Boston Crab Meat", .UnitPrice = 18.4, .UnitsOnOrder = 18})
data.Add(New Product() With {.ProductName = "Konbu", .UnitPrice = 6, .UnitsOnOrder = 24})
Return data
End Function
End Class
Public Class Product
Private privateProductName As String
Public Property ProductName() As String
Get
Return privateProductName
End Get
Set(ByVal value As String)
privateProductName = value
End Set
End Property
Private privateUnitPrice As Double
Public Property UnitPrice() As Double
Get
Return privateUnitPrice
End Get
Set(ByVal value As Double)
privateUnitPrice = value
End Set
End Property
Private privateUnitsOnOrder As Integer
Public Property UnitsOnOrder() As Integer
Get
Return privateUnitsOnOrder
End Get
Set(ByVal value As Integer)
privateUnitsOnOrder = value
End Set
End Property
End Class
End Class
End Namespace
The following code snippets (auto-collected from DevExpress Examples) contain references to the SelectionMode property.
Note
The algorithm used to collect these code examples remains a work in progress. Accordingly, the links and snippets below may produce inaccurate results. If you encounter an issue with code examples below, please use the feedback form on this page to report the issue.
See Also