Selection and Working with Blocks of Cells Begin | Previous | Next
Specifying Cell Ranges | Selection and Selected Cell Ranges | Selection Policy | Spanning | Fill Values and Properties | Fill Values and Properties and Increment Value | Draw a Border around a Group of Cells | Copy and Paste

Specifying Cell Ranges
Most properties on individual cells can be set on a group of cells called a Cell Range with one line of code. The Cell Range consisting of the user selection can be returned by calling:
grid.getCells()
All cells in a given range of rows can be returned by calling:
grid.getRows( firstRow, lastRow )
All cells in a given range of columns can be returned by calling:
grid.getColumns( firstColumn, lastColumn )
An arbitrary range of cells can be return by calling:
grid.getCells( firstRow, firstColumn, lastRow, lastColumn )
Examples in the tutorial will use one or more of these types of ranges. It should be noted that the selection is not modified by any of these calls.
Selection and Selected Cell Ranges
The selection can be programatically set by calling two methods:
grid.setRow( row );
grid.setCol( column );
The cell specified by this row and column will be selected and will be referred to as the active cell. To select a block of cells, you will need to call two additional methods after calling the previous ones:
grid.setRow2( endRow );
grid.setCol2( endColumn );
This will select the range of cells beginning with row,column and ending with and including endRow,endColumn. The selected cells can then be returned as a Cell Range by calling:
grid.getCells()
Selection Policy
The selection can be set to only select the entire row:
grid.setSelectionPolicy( DsGrid.SELECTION_BY_ROW );
or to only select the entire column:
grid.setSelectionPolicy( DsGrid.SELECTION_BY_COLUMN );
or to allow any block of cells to be selected:
grid.setSelectionPolicy( DsGrid.SELECTION_FREE );
Spanning
A cell can span over the area of the screen occupied by other cells. These other cells will be hidden but will retain their original value and properties. The cell that has spanned over other cells will become the combined size of the range of cells it is spanning. The span can be created by using cell range such as the selected cells:
grid.getCells().span();
or it can use the underlying call to create the span which in this example starts with cell 1,1 and spans 2 columns and 2 rows.
grid.spanCells( 1, 1, 2, 2 );
Fill Values and Properties
In a given cell range you can fillDown, fillRight, fillUp, or fillLeft. This will take the cells at the beginning of the range and copy the values and properties into the cell going the specified direction. This example will take the values and properties in the beginning row and copy them down into each cell below in the currently selected range of cells.
grid.getCells().fillDown();
Fill Values and Properties and Increment Value
In a given cell range you can fill in any direction and increase the value by a given amount. The direction to fill is set by the order of the cell range. The range can be re-ordered by calling the reOrder method. Then you call fillColumns to propate in a column going up or down depending on the order of the cell range. If the value is not of the appropriate type indicated by the seriesType then it will be coerced to that type. This example will take the values and properties in the beginning row and copy them down into each cell below in the currently selected range of cells. The month for each date will be incremented from the cell above it.
grid.getCells(1,1,3,2).fillColumns(DsGridCellRange.SERIES_MONTH,1,true);
Draw a Border around a Group of Cells
A line border can be drawn around a range of cells. This example draws a green dashed border 2 pt wide around the selected cells.
grid.getCells().outline( Color.green, DsConstants.DASH, 2 );
Selectively Draw Grid Lines
The grid lines can be set to be drawn only in certain areas of the grid. The easiest way is to turn off the ShowGrid property then selectively turn on the grid line for certain cells.
grid.setShowGrid( false );
grid.getCells( 1, 1, 2, 1 ).setShowGrid( true );
More flexibility can be had by setting the border properties individually. For example, if you wanted to display the grid with dashed lines.
grid.setShowGrid( false );
grid.setGridColor( Color.blue );
grid.getCells( 1, 1, 2, 1 ).setBorderStyle( DsCell.GRID_DOT );
grid.getCells( 1, 1, 2, 1 ).setBorderLines( DsCell.BORDER_OUTLINE );
Copy and Paste
Copy and paste to and from the clipboard can be executed by the standard keyboard shortcuts Control-C and Control-V. It can also be done programatically:
grid.copyToClipboard();
grid.paste();
Copyright 2003 Diamond Edge, Inc. All rights reserved.