The Basics > Configuring Columns at Run Time > Adding and Removing Columns |
By manipulating the Columns collection, you can add or remove columns from the list at run time. You can even perform complete list configurations in code, rather than using the visual editing features.
Here is an example of how a column can be added to the list using the Columns collection:
Example Title |
Copy Code
|
---|---|
' Create a new Column 0 Dim C As TrueDBList80.Column Set C = TDBList1.Columns.Add(0) ' Initialize the new Column 0 With C .Visible = True ' Make it visible .DataField = "LAST" ' Set the column's database field .Caption = "Last Name" ' Set the column's caption End With ' Make Column 0 as wide as Column 1 C.Width = TDBList1.Columns(1).Width |
Several key points should be noted in this example:
The Columns collection is referenced as TDBList1.Columns, while an individual Column object is referenced with a numeric index, TDBList1.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 list. 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 = TDBList1.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 list's display, use the Remove method. This is the general technique to remove an item from a collection:
Example Title |
Copy Code
|
---|---|
TDBList1.Columns.Remove 1 |
Or, to remove all columns from a list:
Example Title |
Copy Code
|
---|---|
While TDBList1.Columns.Count <> 0 TDBList1.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 list 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 list to the data source in order for the new column binding to take effect.