Use of the DataGrid control in VB (Iv.)

Source: Internet
Author: User
Tags integer ole require

Using Bookmarks and Selbookmarks to track records
Bookmarks and Selbookmarks provide a means of marking records. This is necessary when writing specific features in an application, such as allowing end users to manually select multiple nonadjacent records to make a mass update of the selected records. In these cases, you need to mark which records have been selected, so you can use the Selbookmarks collection and its properties.
There are two functions, the Celltext and Cellvalue methods, that require a tag to execute correctly.

Mark the user's selection
The Selbookmarks collection contains bookmarks for all the selected records. When end users manually select records (that is, hold down the CTRL key when clicked), the bookmarks for each selected record are added to the collection. Using a standard loop, users can know what has been selected, or they can save a bookmark (because a value may need to be recovered), and perform an action:
Dim I as Integer ' counter
Dim intcount as Integer
intcount = datagrid1.selbookmarks.count-1
ReDim ARRSELBK (intcount) ' declares an array to hold the bookmark.
For i = 0 To intcount
ARRSELBK (i) = Datagrid1.selbookmarks (i)
' Do the action here. If the operation must be
' Cancel, exit the loop, and then use the number
' Group to cancel these changes.
Next I

Select records by adding to the Selbookmarks collection in the program
You can also select records in your program by adding records to this collection. For example, you might have a grid that displays all orders for a specified customer. If you want to highlight all records in which the customer spends more than $ $, filter the records and add the resulting bookmarks to the Selbookmarks collection.
Dim RS as Recordset
Set rs = Adodc1.recordset

While not Rs. Eof
If rs! SupplierID = Then
DataGrid1.SelBookmarks.Add Rs. Bookmark
End If
Rs. MoveNext
Wend

Show calculated results fields
Suppose you have a field named "Price" in the table, and you want to use a local tax rate to calculate the tax on each item in the table. This is a computed result field that can be computed by modifying the DataSource query and returning this value to the DataGrid control.
To create a calculated result field in the DataGrid control
1. Confirm that an OLE DB data source has been established for the Northwind database on the machine, and if you have not yet created such a data source, follow the steps in the "Create Northwind OLE dbdata connection."
2. Place an ADO Data control and a DataGrid control on the form.
3. Set the ConnectionString property of the ADO data control to the data source of Northwind.
4. Set the RecordSource property of the ADO Data control. In the Properties window, click Record Source and enter Select ProductName, UnitPrice, (UnitPrice *. 082) as Tax from.
5. Set the DataSource property of the DataGrid control to this ADO Data control.
6. Run the project.

Use the DataGrid control with a class module
If you want to access data that is stored in a custom format or in a format that is not directly supported by an ODBC driver, you can create a class to encapsulate the data. You can then write the custom functions of the class to retrieve the data. This class becomes a data source that can be used by any data consumer, such as the DataGrid control.
in the Initialize event of this class module, first by declaring one as the New ADODB. A variable of the recordset to create a ADODB Recordset object. After you create the Recordset object, add the fields, and each field in each data source is added. This recordset is then populated with the appropriate data.
Note You can also use the OLE DB sample provider to create a data source. For more information about the OLE DB sample provider, see "Creating a part with a data provider". The
class module has a getDataMember event that occurs whenever data consumers, such as the DataGrid control, require data. In this event, the Data parameter is set to the Recordset object created in the Initialize event.
If you want to use this class module, you should create a form with a DataGrid control. In the code for the form's Load event, set the control's DataSource property to this class.
Note that the data class module is not available at design time. For example, if you use the DataGrid control, all available data sources appear in a Drop-down list when the user clicks Data source in the Properties window. But it doesn't have this data class module, it can only be set in code.

Use a class module to create a data source
The following example uses a class module to create a simple data source. The DataGrid control is then bound to the module through the DataSource property.
to create a class for the DataGrid
1. Create a new standard Exe project.
2. Adds a DataGrid control to the form. If the DataGrid control is not in the toolbox, on the Project menu, click Parts, click Microsoft DataGrid Controls, and then click OK.
3. On the Project menu, click References. In the Reference dialog box, click Microsoftactivex Data Objects 2.0 Library.
4. On the Project menu, click Add Class module to add a data class module to the project.
5. In the Project Explorer window, click and select the class icon and press F4 to display the Properties window.
6. In the Properties window, change the name of the class to Namesdata.
7. In the Properties window, click DataSourceBehavior and Change the property to Vbdatasource.
8. In the Declarations section of the class module, create a ADODB recordset variable, as follows:
Option Explicit
Private WithEvents rsnames as ADODB. The recordset
uses WithEvents keywords to declare the variable so that the user can program the events of the Recordset object.
9. In the Initialize event for the class, add the following code:
Private Sub class_initialize ()
Add the name of the new data member to the DataMember collection
' This enables other objects to see these available
Datamembersdatamembers.add "Names"

Set rsnames = New ADODB. RecordSet ' Sets the object variable.
' Creates a Recordset with two fields and opens the recordset.
' first record has the data type of an integer, and the second record is a string that can be up to 256 characters in Maximum
. CursorType is set to Openstatic
'-an updatable snapshot of a set of records. The LockType is set to
' lockoptimistic to allow updates to the recordset.
with Rsnames
. Fields.Append "ID", Adinteger
. Fields.Append "Name", Adbstr, 255
. CursorType = adOpenStatic
. LockType = adLockOptimistic
. Open
End With

Dim I as Integer
For i = 1 to 10 ' Add 10 records.
Rsnames.addnew
Rsnames!id = i
rsnames! Name = "Name" & I
Rsnames.update
Next I
Rsnames.movefirst ' moves to the beginning of the recordset.
End Sub
This section of code first creates a Recordset object and then adds two fields to the object. The code then adds 10 records to the recordset.
10. In the getDataMember event for the class, add the following code:
Private Sub Class_getdatamember (ByVal DataMember as String, _
Data as Object)
Set Data = rsnames
End Sub
Whenever this event occurs-that is, when the class object is bound to a data consumer, such as the DataGrid control, the code returns the Recordset object.
11. In the Form object's code module, declare an object variable for a data class:
Option Explicit
Private Datnames as Namesdata ' class variable
12. In the code of the Form object's Load event, set the DataSource of the DataGrid control to that class object.
Private Sub Form_Load ()
' Create a new Namesdata object
Set datnames = New namesdata
' Bind this DataGrid to a new data source Datnames
Set DataGrid1.DataSource = Datnames
End Sub
13. Press F5 key to run the project.

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.