When a user selects a range of cells, that range of cells can have a separate background color and foreground color to distinguish it from the other cells in the spreadsheet. The range is called a selection. There are many aspects of selections that you can manage programmatically. In code you can add and remove selections and you can find out what is selected. This topic summarizes some of the tasks you can perform with selections in code.
- To add a selection (a range of cells that are displayed as selected), use the Sheets AddSelection method and specify the starting row and column, and the number of rows and columns in the selection.
- To get all the ranges of cells that are presently selected, use the Sheets GetSelections method. To return a specific selection, use the Sheets GetSelection method.
- To remove all of the selections, use the Sheets ClearSelection method. To remove a specific selection, use the Sheets RemoveSelection method and specify the row and column, and the number of rows and columns to remove from the selection.
- To clear all selections when a new active cell is set programmatically, using the Boolean clearSelection parameter in the SetActiveCell method.
- To keep a selection highlighted, use the RetainSelectionBlock property of the FpSpread class.
- You can move a selected cell in the view using the MoveActiveOnFocus property of the FpSpread class.
- To work with events regarding selections, refer to the SelectionChangedEventArgs class.
To select all the cells in a sheet use the RowCount and ColumnCount properties for that sheet, as in this line of code: FpSpread1.ActiveSheet.Models.Selection.SetSelection(0, 0, FpSpread1.ActiveSheet.RowCount, FpSpread1.ActiveSheet.ColumnCount)
The DefaultSheetSelectionModel class (and IDisjointSelection interface) GetSelections method returns -1 for either the RowCount or the ColumnCount if all the cells in that row or column are selected, as when the end user clicks on a header to make a selection.
For information on the underlying model for selections, refer to Understanding the Selection Model.
Return to Customizing User Selection of Data.
Using a Shortcut
To add a selection, use the AddSelection method from the Sheets shortcut, specifying the necessary parameters.
Example
This example code selects two ranges of cells.
C# | Copy Code |
---|---|
// Set the sheet to allow multiple range selections. fpSpread1.Sheets[0].SelectionPolicy = FarPoint.Win.Spread.Model.SelectionPolicy.MultiRange; // Select cells C3 through D4. fpSpread1.Sheets[0].AddSelection(2, 2, 2, 2); // Select cells F6 through H8. fpSpread1.Sheets[0].AddSelection(5, 5, 3, 3); |
VB | Copy Code |
---|---|
' Set the sheet to allow multiple range selections. FpSpread1.Sheets(0).SelectionPolicy = FarPoint.Win.Spread.Model.SelectionPolicy.MultiRange ' Select cells C3 through D4. FpSpread1.Sheets(0).AddSelection(2, 2, 2, 2) ' Select cells F6 through H8. FpSpread1.Sheets(0).AddSelection(5, 5, 3, 3) |
Using Code
To add a selection, use the AddSelection method from the Sheets shortcut, specifying the necessary parameters. Then assign the SheetView object to a sheet in the component.
Example
This example code selects two ranges of cells.
C# | Copy Code |
---|---|
FarPoint.Win.Spread.SheetView newsheet=new FarPoint.Win.Spread.SheetView(); // Add two selections.newsheet.AddSelection(2, 2, 2, 2); newsheet.AddSelection(5, 5, 3, 3); // Assign the SheetView to a sheet in the component. fpSpread1.Sheets[0] = newsheet; |
VB | Copy Code |
---|---|
Dim newsheet As New FarPoint.Win.Spread.SheetView() ' Add two selections. newsheet.AddSelection(2, 2, 2, 2) newsheet.AddSelection(5, 5, 3, 3) ' Assign the SheetView to a sheet in the component. FpSpread1.Sheets(0) = newsheet |