2012-06-26 183 views
0

我們如何使用datagridview更新表格?假設datagridview在運行時是可編輯的?任何形式的幫助表示讚賞。如何使用datagridview更新數據庫?

Dim con As New OleDb.OleDbConnection 
    Dim dbProvider As String 
    Dim dbSource As String 
    Dim ds As New DataSet 
    Dim da As OleDb.OleDbDataAdapter 
    Dim sql As String 

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 

     dbProvider = "PROVIDER=Microsoft.ACE.OLEDB.12.0;" 
     dbSource = "Data Source = C:\record.accdb" 

     con.ConnectionString = dbProvider & dbSource 

     con.Open() 

     sql = "SELECT * FROM StudentRecords" 
     da = New OleDb.OleDbDataAdapter(sql, con) 
     da.Fill(ds, "AddressBook") 
     MsgBox("Database is now open") 

     con.Close() 

     MsgBox("Database is now Closed") 

     DataGridView1.DataSource = ds 
     DataGridView1.DataMember = "AddressBook" 
    End Sub 

回答

2

請按照下列步驟操作:

  1. 迭代使用每個或簡單的For循環
  2. 寫UPDATE語句將更改保存的DataGridView的行集合。