这是我目前的代码,我刚刚开始编码,所以我不太确定这里问的格式,请原谅我。
我的问题是每次我尝试点击更新“记录成功更新”消息框会显示但数据不会更新。我很不确定要改变什么,因为我很确定一切都是正确的。我还尝试在“'CustomerID =”&myid&“”中添加一个实际的ID号,然后它会更新,但我需要它不再需要它。
对不起,如果我不能很好地解释它,请告诉我是否有需要解释的东西。[表格]:https://imgur.com/IKFO5jh
Imports System.Data.OleDb
Public Class Form3
Private Sub DataGridView1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
Dim i As Integer
Dim myid As Integer
With DataGridView1
If e.RowIndex >= 0 Then
i = .CurrentRow.Index
myid = .Rows(i).Cells("CustomerID").Value.ToString
TextBox2.Text = .Rows(i).Cells("CustomerName").Value.ToString
TextBox3.Text = .Rows(i).Cells("Address").Value.ToString
TextBox4.Text = .Rows(i).Cells("Age").Value.ToString
TextBox5.Text = .Rows(i).Cells("Gender").Value.ToString
TextBox6.Text = .Rows(i).Cells("Birthday").Value.ToString
TextBox7.Text = .Rows(i).Cells("CivilStatus").Value.ToString
TextBox8.Text = .Rows(i).Cells("Religion").Value.ToString
TextBox9.Text = .Rows(i).Cells("Citizenship").Value.ToString
TextBox10.Text = .Rows(i).Cells("ContactNo").Value.ToString
End If
End With
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Try
Dim conn As New OleDb.OleDbConnection
Dim squery As New OleDb.OleDbCommand
Dim connStr As String
Dim myid As Integer
connStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\user\Desktop\temp2\Valenzuela10A\Valenzuela10A.mdb"
conn.ConnectionString = connStr
squery.Connection = conn
conn.Open()
squery.CommandText = "Update Customer set CustomerName='" & TextBox2.Text & "',Address='" & TextBox3.Text & "',Age='" & CInt(TextBox4.Text) & "',Gender='" & TextBox5.Text & "',Birthday='" & TextBox6.Text & "',CivilStatus='" & TextBox7.Text & "',Religion='" & TextBox8.Text & "',Citizenship='" & TextBox9.Text & "',ContactNo='" & TextBox10.Text & "' where CustomerID=" & myid & " "
squery.ExecuteNonQuery()
squery.Dispose()
MsgBox("Record successfully updated.", MsgBoxStyle.Information, "Message")
load_data()
conn.Close()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub Form3_Load(sender As Object, e As EventArgs) Handles MyBase.Load
load_data()
End Sub
Public Sub load_data()
Try
Dim conn As New OleDb.OleDbConnection
Dim ds As New DataSet
Dim dt As New DataTable
Dim da As New OleDb.OleDbDataAdapter
conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\user\Desktop\temp2\Valenzuela10A\Valenzuela10A.mdb"
conn.Open()
ds.Tables.Add(dt)
da = New OleDbDataAdapter("Select * from Customer", conn)
da.Fill(dt)
DataGridView1.DataSource = dt
conn.Close()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
End Class
如果要将DataGrid绑定到Access表,请执行此操作。
Imports System.Data.OleDb
Public Class Form1
Dim connetionString As String
Dim connection As OleDbConnection
Dim oledbAdapter As OleDbDataAdapter
Dim oledbCmdBuilder As OleDbCommandBuilder
Dim ds As New DataSet
Dim changes As DataSet
Dim i As Integer
Dim sql As String
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
connetionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Your mdb filename;"
connection = New OleDbConnection(connetionString)
Sql = "select * from tblUsers"
Try
connection.Open()
oledbAdapter = New OleDbDataAdapter(Sql, connection)
oledbAdapter.Fill(ds)
DataGridView1.Data Source= ds.Tables(0)
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Try
oledbCmdBuilder = New OleDbCommandBuilder(oledbAdapter)
changes = ds.GetChanges()
If changes IsNot Nothing Then
oledbAdapter.Update(ds.Tables(0))
End If
ds.AcceptChanges()
MsgBox("Save changes")
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
End Class
http://vb.net-informations.com/dataadapter/dataadapter-datagridview-oledb.htm
还有其他几种方法可以做到这一点。我会留给你探索其他选择。