# Selection

FlexGrid for WinForms provides options related to selection in the grid such as selection modes, get selected cells etc. Know more about grid selection here.

## 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 <span data-popup-content="This property is available in both \u003ca href=\u0022/componentone/docs/win/online-flexgrid/\u0022\u003e.NET\u003c/a\u003e and \u003ca href=\u0022/componentone/docs/win/online-flexgrid/\u0022\u003e.NET Framework\u003c/a\u003e." data-popup-title="SelectionMode" data-popup-theme="ui-tooltip-green qtip-green">SelectionMode</span> property of the <span data-popup-content="This class is available in both \u003ca href=\u0022/componentone/docs/win/online-flexgrid/\u0022\u003e.NET Framework\u003c/a\u003e and \u003ca href=\u0022/componentone/docs/win/online-flexgrid/\u0022\u003e.NET\u003c/a\u003e." data-popup-title="C1FlexGrid" data-popup-theme="ui-tooltip-green qtip-green">C1FlexGrid</span> class. The property accepts values from the <span data-popup-content="This enumeration is available in both \u003ca href=\u0022/componentone/docs/win/online-flexgrid/\u0022\u003e.NET Framework\u003c/a\u003e and \u003ca href=\u0022/componentone/docs/win/online-flexgrid/\u0022\u003e.NET\u003c/a\u003e." data-popup-title="SelectionModeEnum" data-popup-theme="ui-tooltip-green qtip-green">SelectionModeEnum</span> enumeration. Below table gives a quick snapshot of how selection looks like in each of these modes.

| Value | Description | Snapshot |
| ----- | ----------- | -------- |
| Default | Allows selection of continuous batch of cells using mouse or keyboard. Also, lets the user select entire row or column on clicking the respective header. |  |
| Cell | Allows selection of single cell at a time. | ![single cell selection](https://cdn.mescius.io/document-site-files/images/2f10b028-0ae8-4c29-a102-4d67578c339b/images/selection-mode-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/2f10b028-0ae8-4c29-a102-4d67578c339b/images/selection-mode-cellrange.png) |
| Column | Allows selection of single column at a time. | ![single column selection](https://cdn.mescius.io/document-site-files/images/2f10b028-0ae8-4c29-a102-4d67578c339b/images/selection-mode-column.png) |
| ColumnRange | Allows selection of multiple contiguous columns at a time. | ![column range selection](https://cdn.mescius.io/document-site-files/images/2f10b028-0ae8-4c29-a102-4d67578c339b/images/selection-mode-columnrange.png) |
| ListBox | Allows selection of multiple non-contiguous rows using the **Ctrl** key. | ![listbox selection](https://cdn.mescius.io/document-site-files/images/2f10b028-0ae8-4c29-a102-4d67578c339b/images/selection-mode-listbox.png) |
| Row | Allows selection of single row at a time. | ![single row selection](https://cdn.mescius.io/document-site-files/images/2f10b028-0ae8-4c29-a102-4d67578c339b/images/selection-mode-row.png) |
| RowRange | Allows selection of multiple contiguous rows at a time. | ![row range selection](https://cdn.mescius.io/document-site-files/images/2f10b028-0ae8-4c29-a102-4d67578c339b/images/selection-mode-rowrange.png) |
| MultiRange | Allows selection of multiple non-contiguous blocks of cells using the **Ctrl** key. | ![](https://cdn.mescius.io/document-site-files/images/2f10b028-0ae8-4c29-a102-4d67578c339b/images/selection-multirange.png) |

The following code snippet demonstrates how to enable multi range selection in the grid by setting the **SelectionMode** property to MultiRange.

```csharp
c1FlexGrid1.SelectionMode = C1.Win.FlexGrid.SelectionModeEnum.MultiRange;
```

## Set Selection

FlexGrid provides various ways to set the selection through code. You can use any of these methods depending on the requirement such as selecting the single cell, cell range or rows.

| Selection | Method/Property | Sample code |
| --------- | --------------- | ----------- |
| Single cell | Set the [Row](/componentone/docs/win/online-flexgrid/) and [Col](/componentone/docs/win/online-flexgrid/) property. Default value of both of these properties is **1**. Hence, by default, selection is set to the first scrollable cell on top left of the grid. | *csharp* <br>`c1FlexGrid1.Row = 2; c1FlexGrid1.Col = 1; ` |
|  | Call the [Select](/componentone/docs/win/online-flexgrid/)(rowIndex, colIndex) method to select a single cell. | *csharp* <br>`c1FlexGrid1.Select(2, 1); ` |
| Cell range | Set the [RowSel](/componentone/docs/win/online-flexgrid/) and [ColSel](/componentone/docs/win/online-flexgrid/) property. These properties set the selection starting from value set in [Row](/componentone/docs/win/online-flexgrid/) and [Col](/componentone/docs/win/online-flexgrid/) property to the specified row and column. Note that to specify a block selection, you must set **Row** and before setting **RowSel** and **ColSel**. | *csharp* <br>`c1FlexGrid1.Row = 2; c1FlexGrid1.Col = 1; c1FlexGrid1.RowSel = 4; c1FlexGrid1.ColSel = 3; ` |
|  | Call the [Select](/componentone/docs/win/online-flexgrid/)(CellRange, Boolean) method to select a cell range in a single call. | *csharp* <br>`CellRange cellRange = new CellRange(); cellRange.r1 = 2; cellRange.r2 = 4; cellRange.c1 = 1; cellRange.c2 = 3; c1FlexGrid1.Select(cellRange); ` |
| Rows (If SelectionModes = SelectionModesEnum. ListBox) | Set the [Row.Selected](/componentone/docs/win/online-flexgrid/) property to **true** for individual row objects to select the non-continuous rows. | *csharp* <br>`c1FlexGrid1.SelectionMode = SelectionModeEnum.ListBox; c1FlexGrid1.Rows[1].Selected = true; c1FlexGrid1.Rows[4].Selected = true; ` |

## Get Selection

To get selected range of the WinForms FlexGrid, you can use **Selection** property of the <span data-popup-content="This class is available in both \u003ca href=\u0022/componentone/docs/win/online-flexgrid/\u0022\u003e.NET Framework\u003c/a\u003e and \u003ca href=\u0022/componentone/docs/win/online-flexgrid/\u0022\u003e.NET\u003c/a\u003e." data-popup-title="C1FlexGrid" data-popup-theme="ui-tooltip-green qtip-green">C1FlexGrid</span> class.

```csharp
private void Button1_Click(object sender, EventArgs e)
  {
    C1.Win.C1FlexGrid.CellRange cr;
    cr = c1FlexGrid1.Selection;
    MessageBox.Show("Selected range\n" +
    cr.r1 + ":" + cr.c1 + " to " + cr.r2 + ":" + cr.c2);            
   }
```

```vbnet
Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs)
    Dim cr As C1.Win.C1FlexGrid.CellRange
    cr = c1FlexGrid1.Selection
    MessageBox.Show("Selected range" & vbLf & cr.r1 & ":" + cr.c1 & " to " + cr.r2 & ":" + cr.c2)
End Sub        
```

## 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 **BeforeDoubleClick** 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 WinForms FlexGrid cell on a double click.

```csharp
private void C1FlexGrid1_BeforeDoubleClick(object sender, BeforeMouseDownEventArgs e)
  {
    // Disable the default double click
    e.Cancel = true;
    // Enter the edit mode
    c1FlexGrid1.StartEditing();
    // Convert to the TextBox class and select using the SelectAll method
    TextBox tb = (TextBox)c1FlexGrid1.Editor;
    tb.SelectAll();
  }
```

```vbnet
Private Sub C1FlexGrid1_BeforeDoubleClick(ByVal sender As Object, ByVal e As BeforeMouseDownEventArgs)
    ' Disable the default double click
    e.Cancel = True
    ' Enter the edit mode
    c1FlexGrid1.StartEditing()
    ' Convert to the TextBox class and select using the SelectAll method
    Dim tb As TextBox = CType(c1FlexGrid1.Editor, TextBox)
    tb.SelectAll()
End Sub            
```