[Beginology VB.NET] User experience (1): About data binding and update

xiaoxiao2021-03-06  117

The program interface is shown in the figure above, binds the data with DataGrid and binds to TextBox requirements: 1. As the DataGrid (read-only) record pointer moves, the content of TextBox changes with the change 2, to achieve modification, add, delete the operating instructions : For easy operation, all TextBox is named "TEST" corresponding field name ================================ ==============================00 code code as follows: imports systemimports system.reflection

Public Class Frm1 Inherits System.Windows.Forms.Form Dim OleDbConn1 As New System.Data.OleDb.OleDbConnection Dim dataset1As New System.Data.DataSet Dim adapter1 As System.Data.OleDb.OleDbDataAdapter = New System.Data.OleDb.OleDbDataAdapter ( "SELECT * FROM Table 1", OLEDBCONN1) 'Note, here is associated with Adapter1 with Adapter1, automatically generate the corresponding addition, delete, update SQL statement DIM CB_ADAPTER1AS System.Data.OleDb.Data.Oledb. OLEDBCommandbuilder (Adapter1)

Private Sub Frm1_Load (ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load OleDbConn1 .ConnectionString = "Provider = Microsoft.Jet.OLEDB.4.0; Data Source =" d: /test/test.mdb " OLEDBCONN1 .Open () 'pops Adapter1 .fill (DataSet1, "Table 1")' Binds the data to DataGrid Me.DataGrid1.dataSource = DataSet1.tables ("Table 1") The code below the field value to Corresponding TextBox for i = 0 to DataSet1.Tables ("Table 1"). Columns.count - 1 'Depending on the name of TextBox gets the corresponding instance NameStr = "" NameStr = "Test" & Dataset1.tables ("Table 1" ) .Columns.Item (i) .Caption Dim t As Type = Me.GetType Dim f As FieldInfo = t.GetField ( "_" & namestr, BindingFlags.NonPublic Or BindingFlags.Instance Or BindingFlags.Public) Dim MyControl As TextBox = CType (F.GetValue (me), TextBox "Performs a corresponding data binding mycontrol.databindings.add (" TEXT ", DataSet1.Tables (" Table 1 "), DataSet1.Tables (" Table 1) ") .Column.Item (i) .caption) Next 'Turn off data connection OLEDBCONN1.CLOSE () End sub 'Save Operation Private Sub Btnsave_Click (Byval E AS System.EventArgs) handles btnsave.click' Stop current editing.

Me.BindingContext (DataSet1, "Table 1"). EndCurrentedIt () Gets the contents of the change in DataSet DIM Chgobj as new datatable chgobj = ctype ("Table 1"). GetChanges, DataTable) 'If you change If the content is not empty, the save operation if not chgobj is nothing kilobj is nothing life "Open data connection if oledbconn1.state = 0 TTHEN OLEDBCONN1.Open Adapter1.Update (chgobj) dataset1.acceptchanges () DataSet1.Ables () DataSet1.Tables "Table 1"). Clear () adapter1.fill (DataSet1, "Table 1") 'Close Data Connection OLEDBCONN1.CLOSE () End IFend Sub

'Deletion Private Sub btnDelete_Click (ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click If (Me.BindingContext (dataset1, "Table 1"). Count> 0) Then' Me.BindingContext (dataset1 , "Table 1"). Removeat (Me.BindingContext (DataSet1, "Table 1"). Position) Me.BindingContext (Dataset1, "Table 1"). Removeat (me.datagrid1.currentrowindex ()) Endiff

End Sub 'add operation Private Sub btnAdd_Click (ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click Try' clear the current edits Me.BindingContext (dataset1, "Table 1"). EndCurrentEdit () 'execution Add Me.BindingContext (DataSet1, "Table 1"). AddNew () 'Moves the pointer me.bindingcontext (Dataset1, "Table 1"). Position = (ME.BINDINGCONTEXT (DataSet1, "Table 1"). Count 1) Me.DataGrid1.CurrentRowIndex = Me.BindingContext (dataset1, "table 1"). Position Catch eEndEdit As System.Exception System.Windows.Forms.MessageBox.Show (eEndEdit.Message) End Try End Sub 'Private Sub cancel Btncel_Click (Byval e as system.EventArgs) handles btncel.click me.bindingcontext (dataset1, "Table 1"). CancelCurrendedit () end sub ...... End class ======= ============================================================================================================================================================================================================= ============ Experience ================================================ ================

===== 1, according to the name of TextBox, perform data binding: IMPORTS System.Reflection2, OLEDBCommandBuilder (Overview of MSDN below): OLEDBDataAdapter does not automatically generate changes to DataSet make changes to DataSet The data source is coordinated by the SQL statement. However, if you set the selectcommand property of OLEDBDataAdapter, you can create an OLEDBCommandBuilder object that generates a SQL statement to update a single table. The OLEDBCommandBuilder then generates any other SQL statement that is not set. Whenever the DataAdapter property is set, OLEDBCommandBuilder registers itself as a listener for the RowUpdating event. Only one OLEDBDataAdapter can be associated with an OLEDBCommandBuilder object (or opposite). To generate an Insert, Update, or DELETE statement, OLEDBCommandBuilder automatically uses the SelectCommand property to retrieve the desired metadata set. If you change SelectCommand after retrieving metadata (e.g., after the first update), the refreshschema method should be called to update the metadata. OLEDBCommandbuilder also uses the Connection, CommandTimeout, and Transaction properties referenced by SelectCommand. If you have modified any of these properties or replace the SelectCommand itself, users should call Refreshschema. Otherwise, INSERTCOMMAND, UPDATECOMMAND, and DELETECOMMAND properties have preserved their previous values. If you call Dispose, the association of OLEDBCommandBuilder and OLEDBDataAdapter will not use the generated command. OLEDBDataAdapter does not automatically becomes a SQL statement that enables changes to DataSet and associated data source. However, if you set the selectcommand property of OLEDBDataAdapter, you can create an OLEDBCommandBuilder object that generates a SQL statement to update a single table. The OLEDBCommandBuilder then generates any other SQL statement that is not set. Whenever the DataAdapter property is set, OLEDBCommandBuilder registers itself as a listener for the RowUpdating event. Only one OLEDBDataAdapter can be associated with an OLEDBCommandBuilder object (or opposite). To generate an Insert, Update, or DELETE statement, OLEDBCommandBuilder automatically uses the SelectCommand property to retrieve the desired metadata set. If you change SelectCommand after retrieving metadata (e.g., after the first update), the refreshschema method should be called to update the metadata. OLEDBCommandbuilder also uses the Connection, CommandTimeout, and Transaction properties referenced by SelectCommand.

If you have modified any of these properties or replace the SelectCommand itself, users should call Refreshschema. Otherwise, INSERTCOMMAND, UPDATECOMMAND, and DELETECOMMAND properties have preserved their previous values. If you call Dispose, the association of OLEDBCommandBuilder and OLEDBDataAdapter will not use the generated command. 3. Problems in Data Update: Problem: After updating data, a strange problem appears: updated data in DataGrid has the same two logging solutions: a), after updating the record, clear the DataSet, and then fill DataSet1. Tables ("Table 1"). CLEAR () Adapter1.fill (Dataset1, "Table 1") B), another method is: Define DataSet1.Tables ("Table 1") in Form1 LOAD (FRM1_LOAD) Primary key: DataSet1.Tables ("Table 1"). Primarykey = new datacolumn () {DataSet1.tables ("Table 1"). Columns ("Primary Boke")} 'If the automatic number, you can add the following: DataSet1. Tables ("Table 1"). Columns ("Primary Key Field") .autoincrement = True DataSet1.Tables ("Table 1"). Columns ("Primary button") .autoincrementSeed = 1 DataSet1.tables ("Table 1). ColumnS ("Primary button") .autoincrementStep = 1 'To make sure the value in the column is unique, you can set the column value to automatically increment when adding a new row in the table. 'To create an automatically incremented Datacolumn, you can set the column's AutoInCrement property to True. 'Then, DataColumn will start from the value defined in the AutoInCrementSeed property, and, with the added line, the value of the' AutoInCrement column will increase the value in the list of autocrementsTep properties. 'For AutoInCrement columns, it is recommended to set the DataColumn's ReadOnly property to True.

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

New Post(0)