Bound Mode > Unbound Columns > Creating unbound columns |
The first step in using an unbound column is creating the column itself. This may be done at design time by choosing either the Append or Insert command from the grid's visual editing menu. At run time, unbound columns may be added using the Add method of the Columns collection. The column must be given a name by setting its Caption property. At design time, this is done using the Columns property page. At run time, the Caption property of the appropriate Column object is set. When these actions are performed at run time, new columns are not considered as unbound columns until the grid's ReBind method is executed.
Note: If you attempt to insert an unbound column in code, you may need to use the HoldFields method to ensure that the column appears at the desired position within the grid:
Example Title |
Copy Code
|
---|---|
Dim Col As TrueDBGrid80.Column
With TDBGrid1
Set Col = .Columns.Add(1)
Col.Visible = True
Col.Caption = "Unbound"
.HoldFields
.ReBind
End With
|
When the grid needs to display the value of an unbound column, it fires the UnboundColumnFetch event. This event supplies the user with a bookmark and a column index as the means of identifying the grid cell being requested. The third argument to the event is a Variant which by default is Null, but can be changed to any desired value, and will be used to fill the contents of the cell specified by the given bookmark and column index.
Example Title |
Copy Code
|
---|---|
Private Sub TDBGrid1_UnboundColumnFetch(Bookmark As Variant, _ ByVal Col As Integer, Value As Variant) |
When Recordset data is retrieved through the data control, the data is cached by the grid to allow smooth scrolling operations and rapid display. As a result, many rows must be fetched at one time so that they are readily available for display. Internally, the grid uses a Recordset clone when it requests data, thus allowing data to be retrieved without changing the current row of the bound Recordset managed by the Data control. This is important because UnboundColumnFetch may need to fetch data unrelated to the current row position.
For example, suppose a row is being edited and the user scrolls the grid vertically or horizontally. To update the display, the grid will need to fetch the new data that is scrolled into view for all rows and columns on the face of the grid. However, changing the current row would cause an unwanted update to occur. For this reason, the grid will not allow the current row of the grid or Recordset to be changed during the UnboundColumnFetch event, even through implicit means such as the FindFirst method of the Recordset. Similarly, other Recordset operations are prohibited as well during the course of this event.
Given these restrictions, how do you obtain Recordset data in order to set the values of the unbound column? There are several ways, all of which involve the use of a Recordset clone.