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 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.
To add a selection, use the SheetView's AddSelection method from the Sheets shortcut, specifying the necessary parameters.
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); // 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) |
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.
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.SelectionPolicy = FarPoint.Win.Spread.Model.SelectionPolicy.MultiRange; 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.SelectionPolicy = FarPoint.Win.Spread.Model.SelectionPolicy.MultiRange 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 |
Users can format multiple cells simultaneously using the Format Cells dialog at runtime after selecting a range of cells. The BuiltInDialogs class provides FormatCells method that allows you to customize the style settings.
This example code allows you to select multi-range cells, and call the Format Cells dialog to apply the style effects.
C# |
Copy Code
|
---|---|
FarPoint.Win.Spread.Dialogs.BuiltInDialogs.FormatCells(ActiveSheet.Selection).ShowDialog(); |
VB |
Copy Code
|
---|---|
FarPoint.Win.Spread.Dialogs.BuiltInDialogs.FormatCells(ActiveSheet.Selection).ShowDialog() |