Browsing data with a DataGrid

Source: Internet
Author: User
Tags exit count odbc access database oracle database
datagrid| data

Data-related examples:





Data sets

Friend WithEvents Ds1 as Dgdataviewsample.dataset1




Dataset1 is a framework file for the dataset in the project, which includes two tables: Tableperson and Tabletype





Database connection variables

Friend WithEvents ODC as System.Data.OleDb.OleDbConnection




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

For example, a SQL Server database needs to use the Odbcconnection,oracle database using the SQLCONNECTION,ODBC data source for OracleConnection.





Data adapters

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, a SQL Server database needs to use the Odbcdataadapter,oracle database using the SQLDATAADAPTER,ODBC data source for OracleDataAdapter.

The properties of the data adapter can be added through the toolbar, completed as prompted by the wizard, can be configured at design time in the Properties window, and can be set programmatically with code. A data adapter is the equivalent of a data channel that populates the data source with data from the corresponding dataset or datasheet, and then submits the updated data to the source table of the database through the data adapter after the modifications to the dataset or datasheet have been completed. By modifying the appropriate SQL statements, you can programmatically control the data adapter so that it matches 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




In this case, the Data view is used as the data source for the DataGrid. Datasets and datasheets can also be used directly as data sources.


Data initialization:


There are two things you need to do to initialize the interface, one is to initialize the data (in this case, the InitData procedure), populate the data source with data in the data instance, and the other is to set some control properties in the window (in this case the Initctrl procedure) so that the data can be displayed correctly.





Initializing data

Private Sub InitData ()



Try



Odc. Open ()



Catch ex as Exception



MsgBox (ex. Message)



Application.exit ()



End Try







Try



Oddaperson.fill (Ds1.tableperson)



Oddatype.fill (Ds1.tabletype)



Catch ex as Exception



MsgBox (ex. Message)



Application.exit ()



End Try



End Sub







Initializing a Window control

Private Sub Initui ()



Lbtable.selectedindex = 0



Dg. Select (0)



End Sub





Data Browsing Navigation:


Button First

Private Sub Bfirst_click (ByVal sender as System.Object, ByVal e as System.EventArgs) Handles Bfirst.click



Cgi Unselect (DG. Currentrowindex)



Dim DV as DataView



DV = DG. DataSource



If DV. Table.Rows.Count > 0 Then



Cgi Currentrowindex = 0



Cgi Select (DG. Currentrowindex)



End If



End Sub







button on a

Private Sub Bprev_click (ByVal sender as System.Object, ByVal e as System.EventArgs) Handles Bprev.click



Cgi 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







button Next

Private Sub Bnext_click (ByVal sender as System.Object, 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



Cgi Currentrowindex = dv. Table.rows.count-1



Else



Cgi Currentrowindex = DG. Currentrowindex + 1



End If



Cgi Select (DG. Currentrowindex)



End Sub





Button last

Private Sub Blast_click (ByVal sender as System.Object, ByVal e as System.EventArgs) Handles Blast.click



Cgi Unselect (DG. Currentrowindex)



Dim DV as DataView



DV = DG. DataSource



Cgi Currentrowindex = dv. Table.rows.count-1



Cgi Select (DG. Currentrowindex)



End Sub





Data manipulation


button to add

Private Sub Badd_click (ByVal sender as System.Object, ByVal e as System.EventArgs) Handles Badd.click



Dim DV as DataView



Dim Rownew as DataRowView



DV = DG. DataSource



Rownew = dv. AddNew ()



Rownew.endedit ()



Cgi Currentrowindex = dv. Table.rows.count-1



Cgi Select (DG. Currentrowindex)



End Sub







After the call to AddNew adds a new record, the EndEdit is called, which means that the add operation is complete and you can do anything else with the new record. Before this call, the new record is considered to be the record being edited and will be locked, and the deletion will cause an error, and the record will not actually be stored in the table. After the EndEdit call, the new record is stored in the table, but the row status is flagged as a new record.

After this step is completed, users typically enter the values for each field in the new record through the table. In a program, you actually view the process as a modification to the new record.





Button Delete

Private Sub Bdel_click (ByVal sender as System.Object, 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





Button Save

Private Sub Bsave_click (ByVal sender as System.Object, ByVal e as System.EventArgs) Handles Bsave.click



Me.updatedata ()



End Sub





Save Child Procedure

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 Addds is nothing Then



Else



Oddaperson.update (Addds)



Oddatype.update (Addds)



End If



If Updateds is nothing Then



Else



Oddaperson.update (updateds)



Oddatype.update (updateds)



End If



If Delds is nothing Then



Else



Oddaperson.update (Delds)



Oddatype.update (Delds)



End If



Catch ex as Exception



MsgBox (ex. Message)



Exit Sub



End Try



Try



DVPerson.Table.AcceptChanges ()



DVType.Table.AcceptChanges ()



Ds1.acceptchanges ()



Catch ex as Exception



MsgBox (ex. Message)



Exit Sub



End Try



End Sub







* After you call AcceptChanges for the DataTable, the status flags for all rows are cleared, that is, the program will no longer be able to distinguish which are newly added and which are modified. So all operations that use the row status flag to modify the data source are placed before this call.

* The order in which various modifications are saved back to the data source is the row that was added-the row being modified-that was deleted. To avoid a logical error while the operation occurs.



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.