![]() |
|
Getting started A grid control is a spreadsheet-like control that displays a series of rows and columns. Each cell is referenced by the position (row, column) in the grid. Referencing cells It can have columns headers and rows headers, also called fixed rows and fixed columns. Fixed rows are fixed when you scroll vertically. Fixed columns are fixed when you scroll horizontally. In SCGrid, fixed cells are numbered negative so non-fixed cells always start with 0. For example, the first column of the first fixed row is referenced as (Row,Col) = (-1,0) and the first non-fixed cell is always referenced as (0,0). Thus, when adding a fixed row or a fixed column, you dont need to recalculate the position of non-fixed cell.
Designing the grid At design, you can quickly configured the appearance and the behavior of the grid. You can set the number of rows, columns, the background color, the line color, font, etc...
Adding rows and columns You can do this once at design or you can code in the Form_Load event: SCGrid1.Cols = 8 SCGrid1.Rows = 100 ' coding in this order (Cols then Rows) is more efficient, ' the display will be faster If you dont know, in advance, the number of cols or rows then you must add one column or one row at a time (for example, when data is stored in a file):
' assume you know the number of columns but not the number of rows
i = 0
SCGrid1.Rows = 0
Do While GetDataFromFile (sData)
' a function which retrieves a record in sData
' and returns False if EOF
' in the first iteration, i = 0, you add one row
' but you insert at row zero (non-fixed cell starts at zero)
SCGrid1.Rows = i + 1
' or SCGrid1.InsertRows i
' populate row
SCGrid1.Text (i, 0) = sData.Name
SCGrid1.Text (i, 1) = sData.Address ....
i = i + 1
Loop
Another way (faster), is to assign a number of rows before populating the
grid. The number will be the average maximum of records.
' assume you know the number of columns but not the number of rows i = 0 SCGrid1.Rows = 1000 Do While GetDataFromFile (sData) ' a function which retrieves a record in sData ' and returns False if EOF ' in the first iteration, i = 0, you add one row ' but you insert at row zero (non-fixed cell starts at zero) If SCGrid1.Rows = i Then SCGrid1.Rows = i + 1 ' or If ... Then SCGrid1.InsertRows i ' or also If ... Then SCGrid1.Rows = SCGrid1.Rows + 100 ' populate row SCGrid1.Text (i, 0) = sData.Name SCGrid1.Text (i, 1) = sData.Address .... i = i + 1 Loop SCGrid1.Rows = i ' adjust Rows with the number of recordsTo add fixed cells, you must use FixedCols or FixedRows. The first fixed row (-1) and fixed column (-1) start at, respectively, the bottom and right.
Formatting grid A cell can have its own :
'Right-aligned' + 'bottom-aligned' is correct
'Bottom-aligned' + 'resized to fit the cell' is not correct
'Single line' + 'breaks words' is not correct
Performance Tips and Tricks Set the Redraw property to False whenever you are doing a lot of cells formatting. Add fixed cells before non-fixed cells. And add columns before rows. For example :
With SCGrid1
.Redraw = False
.FixedCols = 1
.FixedRows = 2
.Cols = 8
.Rows = 1000
' here the cells formatting
.Redraw = True
End With
Theoretically, SCGrid can have 32766 rows (including fixed rows) and the same limit for columns. But, performance can be unacceptable depending on the system resources. . |