Code Examples for 10Tec iGrid, Part I
In this section we want to demonstrate how you can solve some particular tasks using the iGrid methods, properties and events. We assume that the name of the grid in our samples is "iGrid1".
How to start - creating columns and rows
Creating some columns and cells you can work with is as simple as ABC. The following two lines of code are used to create a grid with 3 columns and 2 rows:
iGrid1.ColCount = 3
iGrid1.RowCount = 2
Accessing and changing cells
You can access a cell by its row and column indices, and change its contents and format it using a set of corresponding Cell*
properties. For example, here we set the cell text and its color for the cell in the 2nd row and 3rd column:
iGrid1.CellValue(2, 3) = "Some text"
iGrid1.CellForeColor(2, 3) = RGB(255, 0, 0)
Note that you can place values of different types in the iGrid cells, even within the same column:
iGrid1.CellValue(1, 1) = "Text value"
iGrid1.CellValue(2, 1) = 123
iGrid1.CellValue(3, 1) = #9/6/2009#
Easy access by string keys
In iGrid you can assign string keys to rows and columns for easy and understandable usage in your code. The following code creates an 80-pixel width column with the key "ID" and the text "Customer ID" in its header:
iGrid1.AddCol sKey:="ID", sHeader:="Customer ID", lWidth:=80
, and later you can access the cells of this column for instance like this:
iGrid1.CellValue(3, "ID") = 1170
This approach simplifies your code and minimizes your efforts to support your code when the column set is changed as you always access the same column using its constant string key. The same is true for the iGrid rows which keys remain unchanged even after such operations that change row order as sorting.
How to browse an ADO recordset
Despite the fact that the iGrid cells can be populated one-by-one in your code, the control allows you to display prepared data sets in just one method call. To populate iGrid with the data from an rsCustomers ADO recordset, use the FillFromRS
method:
iGrid1.FillFromRS rsCustomers
Once this statement has been executed, your grid displays all the columns and records from your recordset. If you execute the statement above when the grid has some columns, the keys of these columns will be used as field names from your recordset and iGrid will display the data only from those columns.
How to save and restore iGrid layout
The decencies imply that a good application saves the order and width of each column of every grid and restores these parameters when the user launches your application next time. iGrid automates this task by providing the LayoutCol
property to developers. To implement such functionality, save anywhere the string value returned by this property, and assign it to this property later when your application is starting next time. For instance, to store the columns layout in the Windows Registry for the current user, write the following line of code in the Form_Unload event for each grid you using in your application:
SaveSetting "MyAppID", "Layout", "Columns", iGrid1.LayoutCol
Then you can restore columns layout for that user in the Form_Load event using the following statement:
iGrid1.LayoutCol = GetSetting("MyAppID", "Layout", "Columns")
By assigning the stored value to the LayoutCol
property you restore order, width and visibility for each column of your grid. The LayoutSort
property allows you to save and restore the current sorting of the grid (including numerated sort icons) even if you have sorted iGrid by several columns.