The Basics > Configuring Columns at Run Time > Adding and removing columns |
By manipulating the Columns collection, you can add or remove columns from the grid at run time. You can even perform complete grid configurations in code, rather than using the visual editing features.
Here is an example of how a column can be added to the grid using the Columns collection:
Example Title |
Copy Code
|
---|---|
' Create a new Column 0. Dim C As TrueDBGrid80.Column Set C = TDBGrid1.Columns.Add(0) ' Initialize the new Column 0. With C ' Make it visible. .Visible = True ' Set the column's database field. .DataField = "LAST" ' Set the column's caption. .Caption = "Last Name" End With ' Make Column 0 as wide as Column 1. C.Width = TDBGrid1.Columns(1).Width |
Several key points should be noted in this example:
The Columns collection is referenced as TDBGrid1.Columns, while an individualColumn object is referenced with a numeric index, TDBGrid1.Columns(1). All indexes for a collection are zero-based, so index position 1 refers to the second column. This is the general syntax for referencing a collection and its individual elements.
You can add a new Column object to the Columns collection using the collection's Add method, which accepts a numeric index and returns the newly created object. This is the general technique used to add an item to a collection.
The Visual Basic Set statement is needed to store the new column object in the variable C. Without it, the run-time error "Object variable or With block variable not set" will occur.
The newly added column is Column 0 of the grid. The previous Column 0 becomes Column 1, the previous Column 1 becomes Column 2, and so on.
You can insert a new column at any position. For example:
Example Title |
Copy Code
|
---|---|
Set C = TDBGrid1.Columns.Add(3) |
After this statement executes, the new column will be Column 3. The previous Column 3 becomes Column 4, the previous Column 4 becomes Column 5, and so on.
After a new column is added, the Count property of the Columns collection will be automatically incremented by one. You cannot create a column with an index larger than the current value of the Count property. The Count property is read-only, so you cannot append columns by setting it to a larger value.
To delete a member of the Columns collection and remove it from the grid's display, use the Remove method. This is the general technique to remove an item from a collection:
Example Title |
Copy Code
|
---|---|
TDBGrid1.Columns.Remove 1 |
Or, to remove all columns from a grid:
Example Title |
Copy Code
|
---|---|
While TDBGrid1.Columns.Count <> 0 TDBGrid1.Columns.Remove 0 Wend |
At run time, a newly created column is made invisible to avoid unnecessary flicker when multiple columns are created. Therefore, you must explicitly set its Visible property to True. Also, you must set the column's DataField and Caption properties, otherwise the grid will display a blank column with no heading.
Note that when you set the DataField property of a column in code, you must ReBind the grid to the data source in order for the new column binding to take effect.