How to: Obtain Selected Range and Active Cell

  • 2 minutes to read

You can programmatically get or set the selected cell range using the SpreadsheetControl.Selection or Worksheet.Selection property.

One of the cells within a range selected by an end-user is the active cell. When an end-user types something, data is inserted into this cell. Usually, the active cell is the top left cell in the selected range. However, it can be any cell within the current selection. End-user can select a range by dragging a mouse with the CTRL key held down and then change the position of the active cell by clicking within the selected range. To obtain the active cell in code, use the SpreadsheetControl.ActiveCell, SpreadsheetControl.SelectedCell or Worksheet.SelectedCell property.

SpreadsheetControl_Worksheet_Selection

The Selection, SelectedCell and ActiveCell properties of the SpreadsheetControl object provide access to the selected cell range and active cell in the active worksheet. To specify the selected range and active cell in any other worksheet, use the Selection and SelectedCell properties of the corresponding Worksheet object. The following example demonstrates how to set borders around the cell range that is currently selected by an end-user, and apply a color to the active cell.

static void SelectedCell(SpreadsheetControl control)
{
    control.BeginUpdate();

    control.SelectedCell.FillColor = Color.LightGray;
    CellRange c = control.SelectedCell;
    c.FillColor = Color.Blue;

    CellRange currentSelection = control.Selection;
    Formatting rangeFormatting = currentSelection.BeginUpdateFormatting();
    rangeFormatting.Borders.SetOutsideBorders(DevExpress.Utils.DXColor.Green, BorderLineStyle.MediumDashDot);
    currentSelection.EndUpdateFormatting(rangeFormatting);

    control.EndUpdate();
}