我在更新datagridview上的记录时遇到问题。当我试图更新表格2中datagridview中的记录时,记录会发生变化但是form1中的datagridview不会更新,我对记录所做的更新会保留(永久,但只是在表格2中的datagridview 1上。它不会显示在datagridview形式1)。
表单1上的 oprdg = datagridview1
,下面的代码是表单2中更新的代码
这是我的代码:
Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
provider = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source ="
'Change the following to your access database location
dataFile = "C:\Users\DELL\Downloads\ODRNEWSYSTEM\ODRNEWSYSTEM\Database1.accdb"
connString = provider & dataFile
myConnection.ConnectionString = connString
myConnection.Open()
Try
Me.Validate()
Me.OPRBindingSource.EndEdit()
Me.OPRTableAdapter.Update(Me.Database1DataSet.OPR)
MsgBox("Update successful")
myConnection.Close()
Catch ex As Exception
MsgBox("Update failed")
End Try
End Sub
我尝试了一些代码,如下面的代码,但发生错误,错误指向" da.fill(dt)"部分..我忘记了错误是什么。
Private Sub RefreshData()
Dim connString As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\DELL\Downloads\ODRNEWSYSTEM\ODRNEWSYSTEM\Database1.accdb"
Dim myConnection As OleDbConnection = New OleDbConnection()
myConnection.ConnectionString = connString
Dim da As New OleDb.OleDbDataAdapter("SELECT OPROffice as [OPROffice], " & _
"ID as [ID], Elements,Objectives, Measures,Baseline_as_of_2013,Target_by_2016,Actual_Accomplishments_2014,Actual_Accomplishments_2015,Actual_Accomplishments_2016,Initiatives " & _
" FROM OPR ORDER BY ID", myConnection)
Dim dt As New DataTable
'fill data to datatable
da.Fill(dt)
'offer data in data table into datagridview
Me.OPRDataGridView.DataSource = dt
Form1.oprdg.DataSource = dt
'close connection
myConnection.Close()
End Sub
这是另一个代码,错误就像上面代码中的错误一样:
Public Sub Gridrefresh(ByVal s As String, ByVal dgv As DataGridView)
Dim dt As New DataTable()
Dim cmd As New OleDbCommand("select * from " + s + "", myConnection)
cmd.CommandType = CommandType.Text
Dim da As New OleDbDataAdapter(cmd)
da.Fill(dt)
OPRDataGridView.DataSource = dt
End Sub
这是我系统中问题的图像:
答案 0 :(得分:1)
在为数据适配器分配连接并进行填充之前,您需要 myConnection.Open()
使用你的第一个例子btnUpdate_Click有代码吗?
查看文章https://msdn.microsoft.com/en-us/library/system.data.common.dataadapter.update%28v=vs.110%29.aspx
答案 1 :(得分:0)
我找到了答案。我试图使用这段代码:
Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
Try
Dim dt As New DataTable
Dim str As String = String.Empty
provider = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source ="
'Change the following to your access database location
dataFile = "C:\Users\DELL\Downloads\ODRNEWSYSTEM\ODRNEWSYSTEM\Database1.accdb"
connString = provider & dataFile
myConnection.ConnectionString = connString
myConnection.Open()
Me.Validate()
Me.OPRBindingSource.EndEdit()
Me.OPRTableAdapter.Update(Me.Database1DataSet.OPR)
Dim cmd = New OleDb.OleDbCommand("UPDATE [OPR] SET [OPROffice] = @OPROffice, [Elements] = @Elements, [Objectives] = @Objectives, [Measures] = @Measures, [Baseline_as_of_2013] = @Baseline_as_of_2013, [Target_by_2016] = @Target_by_2016, [Actual_Accomplishments_2014] = @Actual_Accomplishments_2014, [Actual_Accomplishments_2015] = @Actual_Accomplishments_2015, [Actual_Accomplishments_2016] = @Actual_Accomplishments_2016, [Initiatives] = @Initiatives Where ID = @ID", myConnection)
cmd.Parameters.AddWithValue("@OPROffice", OPROfficeTextBox.Text)
cmd.Parameters.AddWithValue("@Elements", ElementsTextBox.Text)
cmd.Parameters.AddWithValue("@Objectives", ObjectivesTextBox.Text)
cmd.Parameters.AddWithValue("@Measures", MeasuresTextBox.Text)
cmd.Parameters.AddWithValue("@Baseline_as_of_2013", Baseline_as_of_2013TextBox.Text)
cmd.Parameters.AddWithValue("@Target_by_2016", Target_by_2016TextBox.Text)
cmd.Parameters.AddWithValue("@Actual_Accomplishments_2014", Actual_Accomplishments_2014TextBox.Text)
cmd.Parameters.AddWithValue("@Actual_Accomplishments_2015", Actual_Accomplishments_2015TextBox.Text)
cmd.Parameters.AddWithValue("@Actual_Accomplishments_2016", Actual_Accomplishments_2016TextBox.Text)
cmd.Parameters.AddWithValue("@Initiatives", InitiativesTextBox.Text)
cmd.Parameters.AddWithValue("@ID", IDTextBox.Text)
cmd.ExecuteNonQuery()
Catch ex As Exception
MsgBox("Update failed")
Finally
myConnection.Close()
End Try
RefreshData()
OPRDataGridView.RefreshEdit()
End Sub
Private Sub RefreshData()
Dim connString As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\DELL\Downloads\ODRNEWSYSTEM\ODRNEWSYSTEM\Database1.accdb"
Dim myConnection As OleDbConnection = New OleDbConnection()
myConnection.ConnectionString = connString
Dim da As New OleDb.OleDbDataAdapter("SELECT OPROffice as [OPROffice], " & _
"ID as [ID], Elements,Objectives, Measures,Baseline_as_of_2013,Target_by_2016,Actual_Accomplishments_2014,Actual_Accomplishments_2015,Actual_Accomplishments_2016,Initiatives " & _
" FROM OPR ORDER BY ID", myConnection)
Dim dt As New DataTable
'fill data to datatable
da.Fill(dt)
'offer data in data table into datagridview
Me.OPRDataGridView.DataSource = dt
Form1.oprdg.DataSource = dt
'close connection
myConnection.Close()
End Sub