[]
        
(Showing Draft Content)

Selection

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 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.

single cell selection

CellRange

Allows selection of continuous batch of cells using mouse or keyboard.

cell range selection

Column

Allows selection of single column at a time.

single column selection

ColumnRange

Allows selection of multiple contiguous columns at a time.

column range selection

ListBox

Allows selection of multiple non-contiguous rows using the Ctrl key.

listbox selection

Row

Allows selection of single row at a time.

single row selection

RowRange

Allows selection of multiple contiguous rows at a time.

row range selection

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.

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 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.

csharp

c1FlexGrid1.Row = 2; c1FlexGrid1.Col = 1;


Call the Select(rowIndex, colIndex) method to select a single cell.

csharp

c1FlexGrid1.Select(2, 1);

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.

csharp

c1FlexGrid1.Row = 2; c1FlexGrid1.Col = 1; c1FlexGrid1.RowSel = 4; c1FlexGrid1.ColSel = 3;


Call the Select(CellRange, Boolean) method to select a cell range in a single call.

csharp

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 property to true for individual row objects to select the non-continuous rows.

csharp

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 C1FlexGrid class.


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);            
   }
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.


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();
  }
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            

Show Selection Statistics

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:

  • Average: Displays the value of the non-empty cells in a range.

  • Sum: Displays the sum of all values in the range.

  • Clear: Clear the existing aggregates

  • Count: Displays the total number of non-empty cells in a range.

  • CountDistinct: Displays the count of unique non-empty cells in a range.

  • Min: Displays the minimum value in a range.

  • Max: Displays the maximum value in a range.

  • Percent: Displays the percentage value of the grand total.

  • Std: Displays the sample standard deviation of the values in a range.

  • Var: Displays the sample variance of the values.

  • StdPop: Displays the population standard deviation of the values in a range (uses the formula based on n).

  • VarPop: Displays the population variance of the values in a range (uses the formula based on n).

  • Aggregate: No aggregate.

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.

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;
}