Use of the DataGrid control in VB (III.)

Source: Internet
Author: User
Tags integer split

Working with columns
You can change the data displayed in the DataGrid control dynamically by changing the DataSource property. For example, you can display different tables for the same database. If you do this, the DataGrid control will display data only based on the default properties.

Add, remove, or hide columns
You can add, remove, or hide columns in your program by using the properties and methods of the Columns collection and Column objects.

Add and remove a column
To add a column at run time, you can use the Add method. If you declare a variable first and assign the new object to the variable, you can set various properties in concise code.
Private Sub AddColumn ()
' Add a column to the rightmost position. Then set its Visible, Width,
' Caption and alignment properties. The DataField property specifies
' Which field the column will be bound to.
Dim C as Column
Set C = DataGrid1.Columns.Add (DataGrid1.Columns.Count)
With C
. Visible = True
. Width = 1000
. Caption = "My new column"
. DataField = Adodc1.Recordset.Fields ("ProductName"). Name
. Alignment = Dbgright
End With
End Sub
You can use a method to delete any column. Make sure that you use the Colindex parameter to specify the column you want to delete. The following code deletes the column that is clicked.
Private Sub Datagrid1_headclick (ByVal colindex as Integer)
DataGrid1.Columns.Remove Colindex
End Sub

Hide a column
You can hide any column by setting the Visible property to False. This feature is especially useful when you want to limit the Lie Shi that users can view or edit. The following example loops through the Columns collection, hiding all columns except a few columns.
Private Sub Hidecolumns ()
' Use the DataField property to determine which column is being tested.
' Displays only three columns: ProductName, UnitPrice, and
' UnitsInStock.
Dim C as Column
For each C in Datagrid1.columns
Select Case C.datafield
Case "ProductName"
C.visible = True
Case "UnitPrice"
C.visible = True
Case "UnitsInStock"
C.visible = True
C.caption = "In-Stock" Change the header of this column.
Case Else ' hides all other columns.
C.visible = False
End Select
Next C
End Sub

Manipulating the DataGrid view
A "split" grid allows end users to have multiple views of the same data. For example, suppose you have a large table consisting of 10 fields. In this case, the recordset you view in the control will have a 10 column width, unless the form is very wide, the user will not be able to see the contents of all the columns at the same time. , and assume that the user is interested in only the first column and the last column (for example, the first column is the name and the last column is the phone number). You can split the grid in order to see the columns at both ends without rearranging the columns.

Create a Split object
At design time, you can create a split by right-clicking the grid, clicking Edit, Right-clicking, and then clicking Split. You can edit this split by right-clicking the control and clicking Properties to display the Property Pages dialog box. You can use the Split tab to customize the split. To delete a split, right-click the split, and click Delete.
At run time, the end user can also split the grid manually by clicking the tab at the right bottom left of the grid control (unless this operation is not allowed), as shown in the following illustration:

By default, the DataGrid control contains a Split object. The code to prevent end users from adding splits is:
Datagrid1.splits (0). allowsizing = False

Add and remove splits in a program
The DataGrid control contains a collection of Split objects. To add a split in your program, you can use the Add method as follows:
DATAGRID1.SPLITS.ADD 1
Note The Add method requires a new split index as its parameter. To add a split, set this index parameter to the Count property value of the splits collection.
Using the Add method of the split set, you can add a split in your program as you actually want. Because adding more than two splits will make the grid difficult to use, you can use the collection's Count property to limit the number of splits.
If DataGrid1.Splits.Count < 3 Then ' add a split.
DATAGRID1.SPLITS.ADD DataGrid1.Splits.Count
End If

To synchronize the split
When you split more than one, you may want to control how these splits are scrolled. For example, in a grid with three splits, you can decide to have only the first and third split synchronized, while the intermediate splits are scrolled independently. To synchronize any two (or more) splits, simply set the Scrollgroup property of each split object to the same value.
' Synchronize the first and third Split objects.
With DATAGRID1
. Splits (0)
. Scrollgroup = 1
. Splits (1). Scrollgroup = 2
. Splits (2). Scrollgroup = 1
End With
You can further customize the appearance of a split by setting the Scrollbars property so that the synchronized split group displays only one scroll bar.

Control the behavior of the tab and arrow keys
Use the WrapCellPointer, Tabacrosssplits, and TabAction properties to determine the behavior of the grid when the end user presses the TAB key or arrow key. The
has the highest TabAction property level in these three properties, and it determines whether the WrapCellPointer and Tabacrosssplits properties are valid. TabAction has three set values: Controlnavigation, Column navigation, and Grid navigation. When this property is set to Controlnavigation, press the TAB key to switch focus to the next control according to TabIndex. This setting takes precedence over WrapCellPointer and tabacrosssplits. The
WrapCellPointer property determines the behavior of the TAB key and the arrow keys in any single split. If this property is set to True and the current cell is in the last column, the end user presses the TAB key to make the next row in the first column the current cell. However, if the current cell is in the last column of the last row, then there is no place to "wrap". The
Tabacrosssplits property determines the behavior of two or more split-time tab and arrow keys in the grid. If this property is set to True and the current cell is in the last column of any split, pressing the TAB or arrow key causes the current cell to "jump" to the next split column. The current cell remains the same row position.
Note If both the WrapCellPointer and Tabacrosssplits properties are set to True, the line wraps only when the current cell is in the last column of the last split. The current cell is then swapped to the next row in the first column that is split.

Custom Column Collection
Each Split object has a Columns property that allows the user to manipulate a collection of Column objects. By doing this, you can change the appearance of each Split object. For example, you can use a split to include two columns that display the last-name and first-names fields, and the second split to display phone fields and address fields. To achieve this, you need to set the Visible property of each of the other columns to False, as follows:
' Enumerate Columns collections, DataField properties for each Column object
' For testing. If the test fails, the column is hidden.
Dim I as Integer

' Hides all columns except the ProductName column.
For i = 0 to datagrid1.splits (0). Columns.count-1
If datagrid1.splits (0). Columns (i). DataField <> "ProductName" Then
Datagrid1.splits (0). Columns (i). Visible = False
End If
Next I

' Hides all columns except the UnitPrice column.
For i = 0 to datagrid1.splits (0). Columns.count-1
If datagrid1.splits (1). Columns (i). DataField <> "UnitPrice" Then
Datagrid1.splits (1). Columns (i). Visible = False
End If
Next I

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.