我在更新我的datagridview上的記錄時遇到問題。當我試圖更新表格2中的datagridview中的記錄時,記錄更改,但form1中的datagridview不會更新,我在記錄上進行的更新保留(永久性,但僅在表單2中的datagridview 1上。它不會顯示錶單1中的datagridview)。記錄不會在datagridview中更新
oprdg = datagridview1
的形式1,下面的代碼是在形式上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
和這裏的圖像在我的系統的問題:
http://i.stack.imgur.com/QgNNq.png
爲什麼不使用數據集而不是數據表? – Gino
@Gino,沒有什麼變化的兄弟.. –
你用這個可怕的Try/Catch來隱藏你自己的錯誤。 – Plutonix