Tutorials > Tutorial 15 - Creating a Grid with Fixed, Nonscrolling Columns |
Often, you would like to prevent one or more columns from scrolling horizontally so that they will always be in view. The Splits collection of True DBGrid provides a generalized mechanism for defining groups of adjacent columns, and can be used to implement any number of fixed, nonscrolling columns. In this tutorial, you will learn how to write code to create a grid with two splits, and then "fix" a pair of columns in the leftmost split.
1. Follow steps 1 through 5 of Tutorial 1 to create a project with a TDBGrid bound to a Data control.
2. In the Form_Load event, place the following code to create an additional split and to fix columns 0 and 1 in the leftmost split:
Example Title |
Copy Code
|
---|---|
Private Sub Form_Load() ' Before modifying the grid's properties, make sure the grid is ' initialized by refreshing the Data control. Data1.Refresh ' Create an additional split. Dim S As TrueDBGrid80.Split Set S = TDBGrid1.Splits.Add(0) ' Hide all columns in the leftmost split, Splits(0), except for columns 0 and 1. Dim C As TrueDBGrid80.Column Dim Cols As TrueDBGrid80.Columns Set Cols = TDBGrid1.Splits(0).Columns For Each C In Cols C.Visible = False Next C Cols(0).Visible = True Cols(1).Visible = True ' Configure Splits(0) to display exactly two columns, and disable resizing. With TDBGrid1.Splits(0) .SizeMode = dbgNumberOfColumns .Size = 2 .AllowSizing = False End With ' Usually, if you fix columns 0 and 1 from scrolling in a split, you will ' want to make them invisible in other splits. Set Cols = TDBGrid1.Splits(1).Columns Cols(0).Visible = False Cols(1).Visible = False ' Turn off the record selectors in Split 1. TDBGrid1.Splits(1).RecordSelectors = False End Sub |
Run the program and observe the following:
TDBGrid displays data from the Data control as in Tutorial 1.
The two columns (First and Last) in the leftmost split are fixed and cannot be scrolled. In fact, there is no horizontal scroll bar present under the left split. A horizontal scroll bar appears under the rightmost split, allowing users to scroll the columns in this split.
You can use splits to create fixed, nonscrolling columns anywhere within the grid—even in the middle! You can also use splits to present different views of your data. For example, you can create splits which scroll independently (in the vertical direction) so that users may compare records at the beginning of the database with those at the end. For more information, see How to Use Splits.
This concludes Tutorial 15.