Browse data with DataGrid

xiaoxiao2021-03-06  24

Data related instances:

data set

Friend Withevents DS1 As DGDataViewsample.DataSet1

DataSet1 is a data set frame file in the project, including two tables: TablePerson and TableType

Database connection variable

Friend WitHevents ODC as System.Data.Oledb.oledbConnection

This example uses the Access database, so the database connection uses the OLEDBConnection type. For different 3 database types, .NET provides different database connection classes.

For example, the SQL Server database requires SQLCONNECTION, ODBC data sources use ODBCCONNECTION, and Oracle databases need to use OracleConnection.

Data adapter

Friend WitHevents Oddaperson as system.data.oledb.oledbdataAdapter

Friend withevents oddatype as system.data.oledb.oledbdataadapter

Similar to database connections, different data adapter classes are required for different database types.

For example, the SQL Server database requires SqlDataAdapter, the ODBC data source uses ODBCDataAdapter, and the Oracle database needs to use OracleDataAdapter.

The properties of the data adapter can be added by the toolbar, follow the wizard hints; can be configured in the properties window during design; can be set in programming. A data adapter is equivalent to a data channel, which is responsible for popping data in the data source to the corresponding data set or data table, after completing the modification of the data set or data table, then submitting the updated data through the data adapter to Database source table. By modifying the corresponding SQL statement, you can use a programmable control data adapter to match different tables or views in the data source.

This example uses a separate data adapter and data view for each table for convenience.

Data view

Friend Withevents DVPERSON as System.Data.DataView

Friend Withevents DVTYPE As System.Data.DataView

This example uses a data view as a data source of DataGrid. Data sets and data sheets can also be used directly as a data source.

Data initialization:

When the initialization interface, two things need to be done, one is initialization data (in this example is the initdata process), populates the data in the data source into the data instance; the other is some of the control properties in the appropriate settings (this The initctrl process is used in the example to make the data correctly.

Initialization data

Private subinitdata ()

Try

Odc.open ()

Catch exception

MSGBOX (ex.Message)

Application.exit ()

END TRY

Try

Oddaperson.Fill (DS1.TablePerson)

Oddatype.Fill (DS1.TABLETYPE)

Catch exception

MSGBOX (ex.Message)

Application.exit ()

END TRY

End Sub

Initialization window control

Private subini ()

LbTable.selectedIndIndex = 0

Dg.select (0)

End Sub

Data browsing navigation:

First button

Private sub bfirst_click (byvale as system.object, byval e as system.eventargs) handles bfirst.clickdg.unselect (Dg.currentrowindex)

DIM DV AS DATAVIEW

DV = DG.DataSource

If DV.TABLE.ROWS.COUNT> 0 THEN

Dg.currentrowindex = 0

Dg.select (Dg.currentrowIndex)

END IF

End Sub

One button on the button

Private sub bprev_click (Byval E AS System.EventArgs) Handles BPREV.CLICK

Dg.unselect (Dg.currentrowindex)

DIM DV AS DATAVIEW

DV = DG.DataSource

IF DG.Currentrowindex - 1 <= 0 THEN

Dg.currentrowindex = 0

Else

Dg.currentrowindex = dg.currentrowindex - 1

END IF

Dg.select (Dg.currentrowIndex)

End Sub

Under the button

Private sub bnext_click (Byval e as system.EventArgs) Handles Bnext.click

Dg.unselect (Dg.currentrowindex)

DIM DV AS DATAVIEW

DV = DG.DataSource

IF dg.currentrowindex 1> = DV.TABLE.ROWS.COUNT THEN

Dg.currentrowindex = DV.TABLE.ROWS.COUNT - 1

Else

Dg.currentrowindex = DG.CurrenTrowIndex 1

END IF

Dg.select (Dg.currentrowIndex)

End Sub

The last one of the button

Private Sub Blast_Click (Byval E AS System.EventArgs) Handles Blast.click

Dg.unselect (Dg.currentrowindex)

DIM DV AS DATAVIEW

DV = DG.DataSource

Dg.currentrowindex = DV.TABLE.ROWS.COUNT - 1

Dg.select (Dg.currentrowIndex)

End Sub

Data operation

Button Add

Private Sub Badd_Click (Byval E AS System.EventArgs) Handles Badd.click

DIM DV AS DATAVIEW

DIM ROWNEW AS DATAROWVIEW

DV = DG.DataSource

ROWNEW = DV.AddNew ()

Rownew.endedit ()

Dg.currentrowindex = DV.TABLE.ROWS.COUNT - 1

Dg.select (Dg.currentrowIndex)

End Sub

After calling the AddNew Add a new record, the endedit is called, which indicates that this addition operation has been completed, and this new record can be done anything else. Before calling, the new record is considered to be edited, which will be locked, and the deletion of the operation will cause an error, and this record does not real deposit table. After the endedit call, the new record is stored in the table, but the line status is marked as a new record. After this step is completed, the user generally enters the new recorded field value through the table. In the program, it is actually a modification of the new record.

Button delete

Private sub bdel_click (Byval e as system.EventArgs) Handles BDEL.CLICK

DIM DV AS DATAVIEW

DV = DG.DataSource

Dv.delete (Dg.currentrowIndex)

Dg.select (Dg.currentrowIndex)

End Sub

Save button

Private sub bsave_click (Byval E AS System.EventArgs) Handles Bsave.click

Me.Updatedata ()

End Sub

Save child process

Private sub updatedata ()

Dim Addds as DataSet

DIM DELDS AS Dataset

DIM Updateds as DataSet

Try

Addds = DS1.Getchanges (DataRowState.Added)

Delds = ds1.getchanges (DATAROWSTATE.DELETED)

Updateds = DS1.Getchanges (DataRowState.Modified)

IF adds is nothing then

Else

ODDAPERSON.UPDATE (AddDS)

Oddatype.Update (AddDS)

END IF

IF updateds is nothing then

Else

Oddaperson.Update (Updateds)

Oddatype.Update (Updated)

END IF

IF Delds is nothing then

Else

Oddaperson.Update (DELDS)

Oddatype.Update (DELDS)

END IF

Catch exception

MSGBOX (ex.Message)

EXIT SUB

END TRY

Try

DvPerson.table.acceptchanges ()

DVTYPE.TABLE.ACCEPTCHANGES ()

DS1.acceptchanges ()

Catch exception

MSGBOX (ex.Message)

EXIT SUB

END TRY

End Sub

* After calling acceptchange for DataTable, the status flag of all rows will be cleared, that is, the program will not be able to distinguish what is newly added, which is modified. So all the operations that use the row status flag to modify the data source before this call.

* Sequence of various modifications to the data source is: Added rows - modified rows - the deleted row. To avoid logic errors during operation.

转载请注明原文地址:https://www.9cbs.com/read-41041.html

New Post(0)