Spread Windows Forms 12.0 Product Documentation
Understanding the Selection Model
Spread Windows Forms 12.0 Product Documentation > Developer's Guide > Understanding the Underlying Models > Finding More Details on the Sheet Models > Understanding the Selection Model

The selection model includes any of the settings related to ranges of selected cells. The selection model includes methods such as counting the number of selected ranges, adding and removing selections, clearing selections, and finding whether a cell is selected.

To use the underlying selection model, use the methods of the selection model. These include the SetSelection method, for setting cells as selected, and the AddSelection, ClearSelection, and RemoveSelection methods for adding, clearing, and removing selected ranges from the sheet. Refer to the DefaultSheetSelectionModel class for more information on the selection model in general and the methods in particular.

The default implementation of the selection model (DefaultSheetSelectionModel) handles the selection of cells and ranges in the sheet and stores the actual cell and range coordinates for each selection. The SpreadView object handles the user interface for the SheetView object and updates the selection model from various event handlers.

The selection model handles the selection data, including computing the range being selected based on the cell clicked (the anchor cell) and the cell under the mouse pointer. The SheetView.GetSelection method and SheetView.SelectionCount property wrap the ISheetSelectionModel indexer and Count property. When there is no selection in the model, the count is 0 and GetSelection returns null.

Some events cause the anchor cell in the selection model to be set (for example, left mouse button down on a cell) so the selection model has the active cell as a selection. If you enter edit mode then cancel it by pressing the Escape key (Esc), or if you use the keyboard to move the active cell around instead of the mouse, then the selection model might be cleared. You should check the SelectionCount before using GetSelection, and in the case where it returns 0, use the ActiveColumnIndex and ActiveRowIndex properties.

The selection model is saved to the view state only if it contains at least one selection.

For more details, refer to the BaseSheetSelectionModel class, the DefaultSheetSelectionModel class, and the ISheetSelectionModel interface.

For more information on working with selections programmatically, refer to Working with Selections.