0
單按鈕和datagridview。如果按鈕text1(「編輯數據庫」)datagridview讀入數據庫,如果按鈕text2(「ACCEPT CHANGES」)datagridview寫入數據庫,但後者不能發生,無論我做什麼。沒有錯誤只是不更改數據庫文件。無法通過DataGridView更新數據庫 - 無錯
Dim ConnString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=w:\PD_Z.mdb"
Dim SQLString As String = "SELECT * FROM ZARADE"
Dim OleDBConn1 As System.Data.OleDb.OleDbConnection = New System.Data.OleDb.OleDbConnection(ConnString)
Dim DataSet1 As New DataSet()
Dim OleDbDataAdapter1 As System.Data.OleDb.OleDbDataAdapter = New System.Data.OleDb.OleDbDataAdapter(SQLString, OleDBConn1)
OleDBConn1.Open()
OleDbDataAdapter1.Fill(DataSet1, "ZARADE")
DataGridView1.DataSource = DataSet1.Tables("ZARADE")
DataGridView1.Columns.Remove(DataGridView1.Columns(0).Name)
If Button2.Text = "Edit database" Then
DataGridView1.ReadOnly = False
Button2.Text = "ACCEPT CHANGES"
Button2.ForeColor = Color.DarkRed
Button1.Enabled = False
ComboBox1.Enabled = False
Else
Button2.Text = "Edit database"
DataGridView1.ReadOnly = True
Button2.ForeColor = Color.Black
Button1.Enabled = True
ComboBox1.Enabled = True
'Dim cb As New OleDbCommandBuilder(OleDbDataAdapter1) ' THIS ONE DOESN'T WORK
' cb.QuotePrefix = "["
' cb.QuoteSuffix = "]"
' OleDbDataAdapter1.Update(DataSet1.Tables("ZARADE"))
Using con = New OleDbConnection(ConnString) ' THIS ONE DOESN'T WORK TOO
Me.Validate()
OleDbDataAdapter1.Update(DataSet1.Tables("ZARADE"))
DataSet1.AcceptChanges()
End Using
DataSet1.AcceptChanges()
OleDBConn1.Close()
此代碼在Button_click事件中。
那麼,我該怎麼做。我以爲首先點擊將數據庫讀入數據網格視圖,然後點擊寫入數據庫。你能給我一些建議嗎? –
我不明白你要存檔的內容...如果你直接在閱讀之後編寫,將不會有任何改變被寫入......標準工作流將從db讀取,附加到網格,在網格中進行更改編輯單元格值(這將自動更改爲行狀態),然後更新到db(例如在按鈕單擊事件中) – PrfctByDsgn
對不起,我忘了解釋。首先點擊激活/導入數據到datagridview,並啓用datagridview編輯「DataGridView1.ReadOnly = False」,然後我有鼠標點擊事件,每一次點擊「單元格」放入「X」值或刪除它,如果有「X」已經在那了。然後在編輯完成時。再次點擊同一個按鈕,將datagridview(「X」)的更改寫入數據庫。上面的代碼只是按鈕單擊事件,它應該填充datagridview並啓用它進行編輯,然後再單擊將這些更改寫入數據庫。 我希望我不會感到困惑。 –