Use the DataGrid Control

Source: Internet
Author: User

 

The DataGrid Control is a binding control similar to the electronic data table. It can display a series of rows and columns to indicate records and fields of the recordset object. You can use the DataGrid to create an application that allows end users to read and write data to the vast majority of databases.Program. The DataGrid control can be quickly configured during design, with only a fewCodeOr no code is required. When the datasource attribute of the DataGrid Control is set during design, the control is automatically filled with the record set of the data source and the column header of the control is automatically set. Then you can edit the columns of the grid, delete, reschedule, add column headers, or adjust the width of any column.
During running, you can switch datasource in the program to view different tables, or modify the query of the current database to return a different set of records.
Note that the DataGrid Control is code compatible with DBGrid in visualbasic5.0. Except for one, the DataGrid control does not support the "Unbind mode" concept of DBGrid. The DBGrid control is included in the tools directory of Visual Basic.

Possible usage
View and edit data in a remote or local database.
Used together with another data-bound control (such as the datalist Control) to display records of a table using the DataGrid Control, this table is linked to the table displayed by the second data binding control through a public field.

Design-time features of the DataGrid Control
You can create a database application by using the design-time features of the DataGrid Control without writing any code. The following describes the general steps for implementing a typical application of the DataGrid Control. For complete step-by-step instructions, see the topic "DataGrid solution 1: create a simple database application using the DataGrid Control ".
To implement a DataGrid Control during design
1. Create a Microsoft Data Link (. MDL) file for the database to be accessed. See the topic "create northwindoledb Data Link" for an example.
2. Place an adodata control on the form and set its connectionstring attribute to the oledb data source created in step 1.
3. Input a recordsource attribute in this adodata control to return a record
SQL statement of the dataset. For example, select * frommytablenamewherecustid = 12
4. Place a DataGrid Control on the form and set its datasource property to this adodata control.
5. Right-click the DataGrid Control and click "retrieve field ".
6. Right-click the DataGrid Control and click Edit ".
7. Reset the size of the grid, delete or add columns to the grid.
8. Right-click the DataGrid Control and click "properties ".
9. Use the "property page" dialog box to set the appropriate properties of the control and configure the grid as the desired appearance and behavior.

Change the displayed data at runtime
After creating a grid that is designed for use, you can also dynamically change the data source of the grid at runtime. The following describes how to implement this function.

Change the recordsource of datasource
The most common way to change the displayed data is to change the datasource query. For example, if the DataGrid control uses an adodata control as its datasource, overwriting recordsource and refreshing the adodata control will change the displayed data.
The 'adodata control is connected to the 'products' table of the northwind database. Search for all
'Supplierid = 12.
Dimstrqueryasstring
Strquery = "select * fromsupplierswheresupplierid = 12"
Adodc1.recordsource = strquery
Adodc1.refresh

Change datasource
During running, You can reset the datasource attribute to a different data source. For example, you may have several adodata controls, each of which connects to a different database or is set to a different recordsource attribute. You can easily reset datasource from one adodata control to another adodata control:
'Reset datasource to a pubs database,
'Use the adodata control of the authors table.
Setdatagrid1.datasource = adopubsauthors

Rebind datasource
When the DataGrid control is used in a remote database, such as sqlserver, the table structure can be changed. For example, you can add a field to the table. In this case, you can call the rebind method to recreate the mesh based on the new structure. Note: If the layout of this column has been changed during design, the DataGrid Control will try to recreate the current layout, including any empty columns. However, you can force the grid to reset all columns by calling the clearfields method first.

return value from DataGrid
after the DataGrid is connected to a database, you may want to monitor which unit the user has clicked. You can use the rowcolchange event instead of the click event.
privatesubdatagrid1_rowcolchange (lastrowasvariant, byvallastcolasinteger)
'displays the text, rows, and columns of the units you click.
Debug. printdatagrid1.text; datagrid1.row; datagrid1.col
endsub

use the celltext and cellvalue methods
when a column uses the numberformat attribute to set the format, the celltext and cellvalue attributes are useful. The numberformat attribute can change the format of any column containing numbers without changing the actual data format. For example, a given grid contains a column named productid that contains integers. The following code enables the DataGrid to display data in the "P-0000" format. In other words, although the actual value contained in the productid field is "3", the value displayed for this grid will be "P-0003 ".
privatesubform_load ()
datagrid1.columns ("productid "). numberformat = "P-0000"
endsub
to return the actual value in the database, use the cellvalue method, as shown below:
privatesubdatagrid1_rowcolchange (lastrowasvariant, byvallastcolasinteger)
debug. print _
maid ("productid "). cellvalue (datagrid1.bookmark)
endsub
note that the cellvalue used above and the celltext value used below must both use the bookmark attribute as a parameter to ensure the function is correct.
conversely, if you want to return the formatted value of this field, use the celltext method:
privatesubdatagrid1_rowcolchange (lastrowasvariant, byvallastcolasinteger)
debug. print _
maid ("productid "). celltext (maid)
endsub
note that the preceding celltext method is equivalent to using the text attribute of the DataGrid Control.

Next step
To read a step-by-step process of using this control to create a simple application, see "using the DataGrid Control to create a simple database application ", or "Create a DataGrid to connect to the datalist control ".
For more information about split objects and how to program them, see "operate DataGrid View ".

 

Reference: http://www.knowsky.com/336992.html

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.