ReoGrid
DOCUMENT
Selection

Detect selection range change

This event will be raised after selection of worksheet has been changed. For example:

void InitWorksheet() 
{
  var sheet = gridControl.CurrentWorksheet;
  sheet.SelectionRangeChanged += sheet_SelectionRangeChanged;
}

void sheet_SelectionRangeChanged(object sender, RangeEventArgs args)
{
  MessageBox.Show("Selection changed: " + args.Range.ToAddress());
}

Remarks

This event will not be raised during dragging mouse on worksheet to change the selection range, this event raised only when mouse button released. To get selection range changes when mouse dragging, use the SelectionRangeChanging event.

Change default behaviors

Change focus cell move to next row instead of next cell

The property SelectionForwardDirection of worksheet is used to decide where is the next position that focus cell should be moved after finish edit. By default, ReoGrid moves focus cell to next cell in same row.

// move right (next cell)
worksheet.SelectionForwardDirection = SelectionForwardDirection.Right;

164

// move down (cell in the next row)
worksheet.SelectionForwardDirection = SelectionForwardDirection.Down;

165

In addition, by handling the below two events to put the selection in any custom position:

  • Worksheet.SelectionMovedForward (Enter)
  • Worksheet.SelectionMovedBackward (Shift+Enter)

These two events have the property IsCancelled that is used to prevent the built-in default operations, to move selection to custom positions, use the code like below:

worksheet.SelectionMovedForward += (s, e) => e.IsCancelled = true;
worksheet.SelectionMovedBackward += (s, e) => e.IsCancelled = true;

// move selection to another position
worksheet.SelectionRange = new RangePosition(worksheet.SelectionRange.Row + 1, 0, 1, 1);

Change next position by handling keyboard events

You can handle the event BeforeCellKeyDown to receive any key pressed on the worksheet. Set the IsCancelled property to true to prevent the built-in default operation, and move the selection to any custom position instead.

worksheet.BeforeCellKeyDown += (s, e) =>
{
  if (e.KeyCode == Keys.Enter)
  {
    e.IsCancelled = true;
    // always move to first cell of next row
    worksheet.SelectionRange =
      new RangePosition(worksheet.SelectionRange.Row + 1, 0, 1, 1);
  }
};

Change selection mode

It is possible to change the selection mode for every worksheet:

worksheet.SelectionMode = WorksheetSelectionMode.Row;

The following modes are supported:

Enum ValueDescription
NoneDo not allow to select anything on worksheet.
CellOnly allow to select single cell.
RangeAllow to select cell or range.
RowAlways to select one or more entire row at a time.
ColumnAlways to select one or more entire column at a time.

Multiple selections (Ctrl-click)

ReoGrid can keep several non-contiguous selections when AllowMultipleSelection is enabled (hold Ctrl/Cmd while clicking). The active selection is still stored in SelectionRange, and all selected blocks are stored in SelectionRanges.

// enable multiple selection
worksheet.AllowMultipleSelection = true;

// access every selected block
foreach (var range in worksheet.SelectionRanges)
{
  // do something with each range
}

Limitation: built-in edit commands (delete, paste, typing) operate on the active selection (the last clicked range) even when multiple ranges are selected. To apply an operation to all selections, iterate SelectionRanges yourself:

foreach (var range in worksheet.SelectionRanges)
{
  worksheet.MergeRange(range);
  // or worksheet.SetRangeData(range, value);
}

Custom limitation to selection range

The BeforeSelectionRangeChange event is fired when the user attempts to change the selection. Set the IsCancelled property to prevent the built-in operation.

This event also has properties that allow user code to change the selection instead of the default selected range. The example below shows how to limit the number of rows and columns in a selection operation.

Limit selection range maximum rows and columns

Add a handler to event BeforeSelectionRangeChange:

worksheet.BeforeSelectionRangeChange += worksheet_BeforeSelectionRangeChange;

By changing the selection end position to limit the maximum number of rows to 5:

void worksheet_BeforeSelectionRangeChange(object sender, BeforeSelectionChangeEventArgs e)
{
  int rows = Math.Abs(e.StartRow - e.EndRow);

  if (rows >= 5)
  {
    if (e.EndRow > e.StartRow)
      e.EndRow = e.StartRow + 5 - 1;
    else 
      e.EndRow = e.StartRow - 5 + 1;
  }
}

Built-in default keyboard operations

See supported hot-key list.

Change selection style

Property SelectionStyle of worksheet could be used to change the selection style.

Default style

worksheet.SelectionStyle = ReoGridSelectionStyle.Default;

161

Windows Focus Style

worksheet.SelectionStyle = ReoGridSelectionStyle.FocusRect;

162

Hide selection

It's also possible to hide the selection by setting to None:

worksheet.SelectionStyle = ReoGridSelectionStyle.None;

163


Was the content of the page helpful?

© 2012-2025UNVELL Inc.