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 property of the C1FlexGrid class. The property accepts values from the SelectionModeEnum 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. | |
CellRange | Allows selection of continuous batch of cells using mouse or keyboard. | |
Column | Allows selection of single column at a time. | |
ColumnRange | Allows selection of multiple contiguous columns at a time. | |
ListBox | Allows selection of multiple non-contiguous rows using the Ctrl key. | |
Row | Allows selection of single row at a time. | |
RowRange | Allows selection of multiple contiguous rows at a time. | |
MultiRange | Allows selection of multiple non-contiguous blocks of cells using the Ctrl key. |
The following code snippet demonstrates how to enable multi range selection in the grid by setting the SelectionMode property to MultiRange.
C# |
Copy Code
|
---|---|
c1FlexGrid1.SelectionMode = C1.Win.FlexGrid.SelectionModeEnum.MultiRange; |
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 and Col 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. |
|
||||
Call the Select(rowIndex, colIndex) method to select a single cell. |
|
|||||
Cell range | Set the RowSel and ColSel property. These properties set the selection starting from value set in Row and Col property to the specified row and column. Note that to specify a block selection, you must set Row and before setting RowSel and ColSel. |
|
||||
Call the Select(CellRange, Boolean) method to select a cell range in a single call. |
|
|||||
Rows (If SelectionModes = SelectionModesEnum. ListBox) |
Set the Row.Selected property to true for individual row objects to select the non-continuous rows. |
|
To get selected range of the WinForms FlexGrid, you can use Selection property of the C1FlexGrid class.
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.
FlexGrid provides selection statistics, which can be used to display Excel-style data summaries in the footer. It allows you to display the statistics of the basic functions and operations performed on the selected cell range. These functions or operations include aggregate operations such as sum, average, count, maximum number, minimum number, and count distinct which can be applied to the selected cells from the context menu. You can use the Aggregate property of the AggregateDefinition class to provide data summary for the selected cell range.
The Aggregate property uses AggregateEnum enumeration to set the aggregate function to be applied on the cells by setting one of the following values:
To show the selection statistics at the footer of the grid, we have added a ToolStripLabel named 'tslSelectionStatistics' docked at the bottom of the grid. Then, add the below code to the SelChange event of the C1FlexGridBase class. This event fires when the user extends the selection with the mouse in the grid.
C# |
Copy Code
|
---|---|
private void c1FlexGrid1_SelChange(object sender, EventArgs e) { var text = string.Empty; if (!flexGrid1.Selection.IsSingleCell) { text = $"Average: {flexGrid1.Aggregate(AggregateEnum.Average):F2} " + $"Count: {flexGrid1.Aggregate(AggregateEnum.Count)} " + $"Summary: {flexGrid1.Aggregate(AggregateEnum.Sum):F2}"; } //Gets the text to be displayed on the toolstrip label tslSelectionStatistics.Text = text; } |