Home > Software engineering >  The use of the vb datagrid control
The use of the vb datagrid control

Time:09-25

How the datagrid control in vb n rows first m columns get focus? Or when the focus on a line of a column, is a focus to the next column

CodePudding user response:

Using a DataGrid control


DataGrid control is a kind of similar to a spreadsheet bound controls, can display a series of rows and columns to show you the object of records and fields, can be used to create a DataGrid allows end users to read and write to the vast majority of database applications, the DataGrid control can be configured rapidly when the design, only a small amount of code or no code, when setting the DataGrid control in the design of the DataSource property, will use the data source record set to automatically fill the control, and automatically set the column header of the control, then you can edit the grid column; Delete, rearrange, add the column header, arbitrary or adjusting the width of a column,

At runtime, can switch the DataSource in the program to look at a different table, or you can modify the current database query, to return a collection of different records,

Notice the DBGrid DataGrid control with Visual Basic 5.0 is compatible code, with one exception: the DataGrid control does not support the DBGrid "unbound mode" concept, DBGrid controls included in the Visual Basic 98 Tools directory,

May the use of the
View and edit in remote or local data in the database,


With another data binding controls (such as the DataList control) are used in combination, use the DataGrid control to display the record of a table, the table by a public field to link to by the second data-bound controls shown in the table,
When using the DataGrid control design feature
Can not write any code, only through the use of the DataGrid control design features to create a database application, the following instructions summarily illustrates the typical applications of realising the DataGrid control the general steps of the complete step-by-step instructions, please refer to the theme "DataGrid plan 1: use the DataGrid control to create a simple database application",

To achieve a DataGrid control in the design of the

Wants to access the database for you to create a Microsoft data link (UDL) files, please refer to the "create Northwind OLE DB data link" theme, for an example,


An ADO Data control is placed on the form, and the ConnectionString property is set to OLE DB Data source created in step 1,


In this RecordSource attribute of the Ado Data control input a SQL statement will return a recordset, for example,
Select * From MyTableName Where CustID=12

Put a DataGrid controls on the form, and the DataSource property is set to the ADO Data control,


Right-click the DataGrid control, and then click "search fields,"


Right-click the DataGrid control, and then click "edit",


To set the size of the grid, delete, or add the columns of the grid,


Right-click the DataGrid control, and then click "properties",


Use "property page dialog box to set the appropriate properties of the control, the grid configuration for the appearance and behavior,
Change the display of data at runtime
In created a grid when using the design characteristics, can also be dynamically at runtime changes the data source of the grid, the following is introduced the common method of realize this function,

To change the DataSource RecordSource
Changes to the Data of the most common method is to change the DataSource query, for example, if the DataGrid control using an ADO Data control as its DataSource, rewrite RecordSource, refresh the ADO Data control, will change the Data shows,

'connection ADO Data control is the Northwind database
'table, a new query search for all
'SupplierID=12 records,
Dim strQuery As String
StrQuery="SELECT * FROM Suppliers WHERE SupplierID=12"
Adodc1. RecordSource=strQuery
Adodc1. Refresh

To change the DataSource
At runtime, the DataSource property can be reset to a different Data source, for example, you might have several ADO Data control, each control different database connection, or set to different RecordSource properties, can easily be set to a DataSource from an ADO Data control reset to another ADO Data control:

'set the DataSource to to a connection to the Pubs database,
'using ADO Data control, the Authors table
The Set DataGrid1. The DataSource=adoPubsAuthors

To bind the DataSource
When the DataGrid control is used in a remote database, such as used, can change the structure of a table, for example, you can add a field to the table, in this case, you can call the Rebind method according to the new to recreate the grid structure, note that if you have already changed the column when the design layout, the layout of the DataGrid control will try to recreate the current, including any empty column, however, by first call ClearFields method, can force the mesh to reset all the columns,

From the DataGrid return value
After the DataGrid is connected to a database, which one might want to monitor users Click on the unit, you can use RowColChange events - instead of Click event - as shown below, :

Private Sub DataGrid1_RowColChange (LastRow As the Variant, ByVal LastCol As Integer)
'display unit of the user's click text, rows and columns of information,
The Debug. Print DataGrid1. Text; DataGrid1. Row; DataGrid1. Col
End Sub

Method using CellText and CellValue
When a column using NumberFormat attribute set format, CellText and CellValue property is very useful, NumberFormat attributes do not have to change the format you can change any actual data contains the format of the number of columns, for example, given a grid, which contains a called ProductID, contains the integer column, the following code will make the DataGrid with the format of the "P - 0000" to display the data, in other words, although in the ProductID field contains the actual values for the "3", but the grid shows the value will be "P - 0003,"

Private Sub Form_Load ()
DataGrid1. Columns (" ProductID "). The NumberFormat="P - 0000
"End Sub

The actual value, to return to the database contains CellValue method should be used, as shown in the following:

Private Sub DataGrid1_RowColChange (LastRow As the Variant, ByVal LastCol As Integer)
The Debug. Print _
DataGrid1. Columns (" ProductID "). CellValue (DataGrid1. Bookmark)
End Sub

Note CellValue used above and below the CellText value, all need to bookmark property as a parameter, the function is right,

On the contrary, if you want to return to the field the formatted value, should use CellText method:

Private Sub DataGrid1_RowColChange (LastRow As the Variant, ByVal LastCol As Integer)
The Debug. Print _
DataGrid1. Columns (" ProductID "). CellText (DataGrid1. Bookmark)
End Sub

Note the above CellText method is equivalent to using the DataGrid control Texr properties,

The next step
To read about using this control to create a simple application of a gradual process, please refer to "use the DataGrid control to create a simple database application", or "create a connection DataList control DataGrid",

To know about the Split object and how detailed information of its programming, please refer to the "operating the DataGrid view,"

CodePudding user response:

Zhao four teacher, I still don't understand what you said,,

CodePudding user response:

Ha ha, estimated that he also don't understand what you said, anyway I am not understand,

CodePudding user response:

I mean how to put a control of the content (such as text1. Text) imported into the datagrid control of line 3, 5, and, after all, a novice, personal feeling is the focus

CodePudding user response:

Col, Row attribute sample
The examples put the "Here" in the current unit, and then the program unit change into the third row of the third unit, and put the "s" in the cell, try this example, use the "parts" dialog in the tool box to add an MS Flex Grid Control (in the "project" menu, select the "components", then the selected Microsoft Flex Grid Control), and then draw a Grid on the new form, to run the program, please press the F5 key, and then click on the Grid,

 Private Sub Form_Load () 
MSFlexGrid1. Rows=8 'set the Rows and columns,
MSFlexGrid1. Cols=5
End Sub

Private Sub MSFlexGrid1_Click ()
'put the text in the current unit,
MSFlexGrid1. Text="Here"
'put the text in the third row, the third column,
MSFlexGrid1. Col=2
MSFlexGrid1. Row=2
MSFlexGrid1. Text="s"
End Sub
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related