2015-06-10 82 views
1

我在更新我的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

+0

爲什麼不使用數據集而不是數據表? – Gino

+0

@Gino,沒有什麼變化的兄弟.. –

+2

你用這個可怕的Try/Catch來隱藏你自己的錯誤。 – Plutonix

回答

1

在將連接分配給數據適配器並進行填充之前,您需要一個myConnection.Open()

你的第一個例子btnUpdate_Click有代碼有效嗎?

看一看文章https://msdn.microsoft.com/en-us/library/system.data.common.dataadapter.update%28v=vs.110%29.aspx

+0

我的第一個btnUpdate_Click的例子,它沒有工作,EVER .. –

+0

我已經改變了我的代碼..檢查它..沒有改變的問題 –

+1

qaZ D這些數據集真的非常敏感點微軟應該有(他們認爲我想要綁定到他們的Azure雲,不,謝謝?)。你說你沒有改變任何東西,你可以看看是否進入你的數據集設計器配置,並看看你是否有任何紅色的X視圖,表或字段。您只需將項目中的連接更改爲指向測試數據庫,例如。測試存活和後退,現在已經破壞了數據集,並且必須從頭開始重新創建它們。我是修復方面的專家,但是如果可以,請儘量避免運行時解決方法。 –

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