# Selection

## Content



## Selection Modes

FlexGrid, by default, allows to select a continuous batch of cells using mouse or keyboard and entire row or column by clicking the corresponding header. However, the default behavior can be changed to allow selection in units of cell, row, column etc. by using [SelectionMode](/componentone/api/winui/online-winui/dotnet-api/C1.WinUI.GridControl/C1.WinUI.Grid.C1GridControl.html#SELECTIONMODE) property of the **C1GridControl** class. The property accepts values from the [GridSelectionMode](/componentone/api/winui/online-winui/dotnet-api/C1.WinUI.Grid/C1.WinUI.Grid.GridSelectionMode.html) enumeration. Below table gives a quick snapshot of how selection looks like in each of these modes.

| **Value** | **Description** | **Snapshot** |
| --- | --- | --- |
| None | Disables selection of cells in FlexGrid control. | ![disable cell selection](https://cdn.mescius.io/document-site-files/images/2d49eac2-0942-4770-99ef-52b14e6815bd/images/selectionmode-none.png) |
| Cell | Allows selection of single cell at a time. | ![single cell selection](https://cdn.mescius.io/document-site-files/images/2d49eac2-0942-4770-99ef-52b14e6815bd/images/selectionmode-cell.png) |
| CellRange | Allows selection of continuous batch of cells using mouse or keyboard. | ![cell range selection](https://cdn.mescius.io/document-site-files/images/2d49eac2-0942-4770-99ef-52b14e6815bd/images/selectionmode-cellrange.png) |
| Column | Allows selection of single column at a time. | ![single column selection](https://cdn.mescius.io/document-site-files/images/2d49eac2-0942-4770-99ef-52b14e6815bd/images/selectionmode-column.png) |
| ColumnRange | Allows selection of multiple contiguous columns at a time. | ![column range selection](https://cdn.mescius.io/document-site-files/images/2d49eac2-0942-4770-99ef-52b14e6815bd/images/selectionmode-columnrange.png) |
| Row | Allows selection of single row at a time. | ![single row selection](https://cdn.mescius.io/document-site-files/images/2d49eac2-0942-4770-99ef-52b14e6815bd/images/selectionmode-row.png) |
| RowRange | Allows selection of multiple contiguous rows at a time. | ![row range selection](https://cdn.mescius.io/document-site-files/images/2d49eac2-0942-4770-99ef-52b14e6815bd/images/selectionmode-rowrange.png) |
| MultiRange | Allows selection of collection of ranges. | ![](https://cdn.mescius.io/document-site-files/images/2d49eac2-0942-4770-99ef-52b14e6815bd/images/selectionmode-multirange.png) |
| ListBox | Allows selection of non-contiguous rows by ctrl+clicking. | ![](https://cdn.mescius.io/document-site-files/images/2d49eac2-0942-4770-99ef-52b14e6815bd/images/selectionmode-listbox.png) |
| MultiColumns | Allows selection of non-contiguous columns by ctrl+clicking. | ![](https://cdn.mescius.io/document-site-files/images/2d49eac2-0942-4770-99ef-52b14e6815bd/images/selectionmode-multicolumns.png) |

The below code snippet shows how to set selection for a cell range via code using the **SelectionMode** property and [Select](/componentone/api/winui/online-winui/dotnet-api/C1.WinUI.Grid/C1.WinUI.Grid.FlexGrid.Select.html) method.

```csharp
var data = Customer.GetCustomerList(100);
flexGrid1.ItemsSource = data;
// set the selection mode
flexGrid1.SelectionMode = GridSelectionMode.CellRange;
// Set cellRange selection
GridCellRange cellRange = new GridCellRange(2, 4, 1, 3);
flexGrid1.Select(cellRange, true, true);
```

## Get Selection

To get selected range of the WinUI FlexGrid, you can use **Selection** property of the **FlexGrid** class.

```csharp
// Get selection
GridCellRange cr;
cr = flexGrid1.Selection;
var d = "Selected range\n" + cr.Row + ":" + cr.Column + " to " + cr.Row2 + ":" + cr.Column2;
```

## Select Text on Double Click

By default, double click on a grid cell changes the cell state to edit mode and shows the cursor at position of the mouse pointer. However, you can change this behavior and select the cell value on double click of a cell. This can be done by detecting and disabling the double click in **CellDoubleTapped** event. Then, call the **StartEditing** method to enter the edit mode, change the editor to a TextBox and call the **SelectAll** method to select the cell value.

Following code shows how you can select text of the WinUI FlexGrid cell on a double click.

```csharp
private void FlexGrid1_CellDoubleTapped(object sender, GridInputEventArgs e)
{
    // Disable the default double click
    e.Cancel = true;
    // Enter the edit mode
    flexGrid1.StartEditing(e.CellRange.Row, e.CellRange.Column, true, true);
    // Convert to the TextBox class and select using the SelectAll method
    TextBox tb = (TextBox)flexGrid1.ActiveEditor;
    tb.SelectAll();
}
```

## Set Selection Style

FlexGrid provides various ways to set the selection style through code. You can use properties like **SelectionBackground** and **SelectionForeground** in FlexGrid class depending on the requirement such as selecting the single cell, cell range or rows with **SelectionMode** enumeration.

```csharp
// set selection BackColor and ForeColor
flexGrid1.SelectionBackground = new SolidColorBrush(Colors.LightYellow);
flexGrid1.SelectionForeground = new SolidColorBrush(Colors.Red);
```

The table below depicts the snapshots of the FlexGrid control for the different values of SelectionMode enumeration when the SelectionBackground is LightYellow and SelectionForeground is Red.

| **SelectionMode value** | **Snapshots** |
| --- | --- |
| Cell | ![](https://cdn.mescius.io/document-site-files/images/2d49eac2-0942-4770-99ef-52b14e6815bd/images/setselection-cell.png) |
| CellRange | ![](https://cdn.mescius.io/document-site-files/images/2d49eac2-0942-4770-99ef-52b14e6815bd/images/setselection-cellrange.png) |
| Row | ![](https://cdn.mescius.io/document-site-files/images/2d49eac2-0942-4770-99ef-52b14e6815bd/images/setselection-row.png) |
| RowRange | ![](https://cdn.mescius.io/document-site-files/images/2d49eac2-0942-4770-99ef-52b14e6815bd/images/setselection-rowrange.png) |
| Column | ![](https://cdn.mescius.io/document-site-files/images/2d49eac2-0942-4770-99ef-52b14e6815bd/images/setselection-column.png) |
| ColumnRange | ![](https://cdn.mescius.io/document-site-files/images/2d49eac2-0942-4770-99ef-52b14e6815bd/images/setselection-columnrange.png) |

You can also use the Select(C1.WinUI.Grid.GridCellRange range, bool scrollIntoView, [bool hideSelectionAdorners = False\]) for selecting a cell range.

```csharp
// Set cellRange selection
GridCellRange cellRange = new GridCellRange(2, 4, 1, 3);
flexGrid1.Select(cellRange, true, true);
flexGrid1.SelectionAdornerBorderBrush = new SolidColorBrush(Colors.GreenYellow);
```