2015-05-13 69 views
3

我建立一個形式,我需要以編程方式設置綁定和數據集(因爲它是可變的,不同的文檔可以在它被加載)。我設法形式中的任何信息加載到DataGridView,同時裝入從DataGridView信息在一些文本框爲結構化編輯:數據保存到數據集已成立了以編程

Example Of Form

但是我努力讓編輯的信息保存回數據庫。它甚至不會用任何東西更新DataGridView。下面是我目前使用的代碼:

Imports System.Data.SqlClient 
Imports System.Data.OleDb 

Module DataGridView_Setup 

    Public Sub Set_Datasource(mode As Integer) 


     Dim connString As String = My.Settings.Database_String 
     Dim myConnection As OleDbConnection = New OleDbConnection 
     myConnection.ConnectionString = connString 
     ' create a data adapter 
     Dim da As OleDbDataAdapter = New OleDbDataAdapter("SELECT ID, [Name Of Person], [SAP Job Number], [Site Name], [Asset Description], [Spares Supplier], [Supplier Contact Name], [Supplier Contact Phone Number], [Supplier Contact Email], [Spares Description], [Part Number], [Quantity To Order], Cost, [Request Date], [Date Ordered], [Ordered By], [Invoice Received], [Invoice Paid], [Method Of Payment], [Date Item Received], [Additional Comments], [Quote Attatchment] FROM Spares", myConnection) 

     ' create a new dataset 
     Dim ds As DataSet = New DataSet 
     ' fill dataset 
     da.Fill(ds, "Spares") 



     Main.DataGridView1.DataSource = ds.Tables(0) 

     Main.DataGridView1.AllowUserToAddRows = False 


     'Set Site Listbox 

     Dim SiteString = My.Settings.SETTINGS_SiteNames 
     Dim SiteBox = Main.VIEW_Site.Items 

     SiteBox.Clear() 

     Do Until SiteString = "" 
      Dim ActiveSiteName = Left(SiteString, InStr(SiteString, "¦")) 
      ActiveSiteName = ActiveSiteName.Remove(ActiveSiteName.Length - 1) 

      With SiteBox 
       .Add(ActiveSiteName) 
      End With 

      SiteString = Replace(SiteString, ActiveSiteName + "¦", "") 

     Loop 


     'Set DataBindings 
     Main.VIEW_Ref.DataBindings.Clear() 
     Main.VIEW_Ref.DataBindings.Add(New Binding("Text", ds, "Spares.ID", False, DataSourceUpdateMode.OnPropertyChanged)) 

     Main.VIEW_NameOfPerson.DataBindings.Clear() 
     Main.VIEW_NameOfPerson.DataBindings.Add(New Binding("Text", ds, "Spares.Name Of Person", False, DataSourceUpdateMode.OnPropertyChanged)) 

     Main.VIEW_SAPJobNo.DataBindings.Clear() 
     Main.VIEW_SAPJobNo.DataBindings.Add(New Binding("Text", ds, "Spares.SAP Job Number", False, DataSourceUpdateMode.OnPropertyChanged)) 

     Main.VIEW_Site.DataBindings.Clear() 
     Main.VIEW_Site.DataBindings.Add(New Binding("Text", ds, "Spares.Site Name", False, DataSourceUpdateMode.OnPropertyChanged)) 

     Main.VIEW_AssetDesc.DataBindings.Clear() 
     Main.VIEW_AssetDesc.DataBindings.Add(New Binding("Text", ds, "Spares.Asset Description", False, DataSourceUpdateMode.OnPropertyChanged)) 

     Main.VIEW_SparesSupplier.DataBindings.Clear() 
     Main.VIEW_SparesSupplier.DataBindings.Add(New Binding("Text", ds, "Spares.Spares Supplier", False, DataSourceUpdateMode.OnPropertyChanged)) 

     Main.VIEW_SupplierContactName.DataBindings.Clear() 
     Main.VIEW_SupplierContactName.DataBindings.Add(New Binding("Text", ds, "Spares.Supplier Contact Name", False, DataSourceUpdateMode.OnPropertyChanged)) 

     Main.VIEW_SupplierContactNumber.DataBindings.Clear() 
     Main.VIEW_SupplierContactNumber.DataBindings.Add(New Binding("Text", ds, "Spares.Supplier Contact Phone Number", False, DataSourceUpdateMode.OnPropertyChanged)) 

     Main.VIEW_SupplierContactNumber.DataBindings.Clear() 
     Main.VIEW_SupplierContactNumber.DataBindings.Add(New Binding("Text", ds, "Spares.Supplier Contact Phone Number", False, DataSourceUpdateMode.OnPropertyChanged)) 

     Main.VIEW_SupplierContactEmail.DataBindings.Clear() 
     Main.VIEW_SupplierContactEmail.DataBindings.Add(New Binding("Text", ds, "Spares.Supplier Contact Email", False, DataSourceUpdateMode.OnPropertyChanged)) 

     Main.VIEW_SparesDesc.DataBindings.Clear() 
     Main.VIEW_SparesDesc.DataBindings.Add(New Binding("Text", ds, "Spares.Spares Description", False, DataSourceUpdateMode.OnPropertyChanged)) 

     Main.VIEW_PartNumber.DataBindings.Clear() 
     Main.VIEW_PartNumber.DataBindings.Add(New Binding("Text", ds, "Spares.Part Number", False, DataSourceUpdateMode.OnPropertyChanged)) 

     Main.VIEW_QuantityToOrder.DataBindings.Clear() 
     Main.VIEW_QuantityToOrder.DataBindings.Add(New Binding("Text", ds, "Spares.Quantity To Order", False, DataSourceUpdateMode.OnPropertyChanged)) 

     Main.VIEW_CostEach.DataBindings.Clear() 
     Main.VIEW_CostEach.DataBindings.Add(New Binding("Text", ds, "Spares.Cost", False, DataSourceUpdateMode.OnPropertyChanged)) 

     Main.VIEW_DateRequested.DataBindings.Clear() 
     Main.VIEW_DateRequested.DataBindings.Add(New Binding("Text", ds, "Spares.Request Date", False, DataSourceUpdateMode.OnPropertyChanged)) 

     Main.VIEW_DateOrdered.DataBindings.Clear() 
     Main.VIEW_DateOrdered.DataBindings.Add(New Binding("Text", ds, "Spares.Date Ordered", False, DataSourceUpdateMode.OnPropertyChanged)) 

     Main.VIEW_OrderedBy.DataBindings.Clear() 
     Main.VIEW_OrderedBy.DataBindings.Add(New Binding("Text", ds, "Spares.Ordered By", False, DataSourceUpdateMode.OnPropertyChanged)) 

     Main.VIEW_InvoiceReceivedDate.DataBindings.Clear() 
     Main.VIEW_InvoiceReceivedDate.DataBindings.Add(New Binding("Text", ds, "Spares.Invoice Received", False, DataSourceUpdateMode.OnPropertyChanged)) 

     Main.VIEW_InvoicePaidDate.DataBindings.Clear() 
     Main.VIEW_InvoicePaidDate.DataBindings.Add(New Binding("Text", ds, "Spares.Invoice Paid", False, DataSourceUpdateMode.OnPropertyChanged)) 


     DataGridView_Setup.BindingUpdates() 
    End Sub 

    Public Sub BindingUpdates() 
     Dim curr As DataGridViewRow = Main.DataGridView1.CurrentRow 



     Main.VIEW_Ref.Text = curr.Cells("ID").Value 
     Main.VIEW_NameOfPerson.Text = curr.Cells("Name Of Person").Value 
     Main.VIEW_SAPJobNo.Text = curr.Cells("SAP Job Number").Value 
     Main.VIEW_Site.Text = curr.Cells("Site Name").Value 
     Main.VIEW_AssetDesc.Text = curr.Cells("Asset Description").Value 
     Main.VIEW_SparesSupplier.Text = curr.Cells("Spares Supplier").Value 
     Main.VIEW_SupplierContactName.Text = curr.Cells("Supplier Contact Name").Value 
     Main.VIEW_SupplierContactNumber.Text = curr.Cells("Supplier Contact Phone Number").Value 
     Main.VIEW_SupplierContactEmail.Text = curr.Cells("Supplier Contact Email").Value 
     Main.VIEW_SparesDesc.Text = curr.Cells("Spares Description").Value 
     Main.VIEW_PartNumber.Text = curr.Cells("Part Number").Value 
     Main.VIEW_QuantityToOrder.Text = curr.Cells("Quantity To Order").Value 
     Main.VIEW_CostEach.Text = "£" + CStr(curr.Cells("Cost").Value) 
     Main.VIEW_DateRequested.Text = curr.Cells("Request Date").Value 

     'Handle DBNULL From now on 

     If IsDBNull(curr.Cells("Date Ordered").Value) = True Then 
      With Main.VIEW_DateOrdered 
       .Text = "Not Ordered Yet" 
       .BackColor = Color.LightPink 
      End With 

     Else 
      With Main.VIEW_DateOrdered 
       .Text = curr.Cells("Date Ordered").Value 
       .BackColor = Color.White 
      End With 

     End If 

     If IsDBNull(curr.Cells("Ordered By").Value) = True Then 
      With Main.VIEW_OrderedBy 
       .Text = "Not Ordered Yet" 
       .BackColor = Color.LightPink 
      End With 
     Else 
      With Main.VIEW_OrderedBy 
       .Text = curr.Cells("Ordered By").Value 
       .BackColor = Color.White 
      End With 

     End If 

     If IsDBNull(curr.Cells("Invoice Received").Value) = True Then 
      With Main.VIEW_InvoiceReceivedDate 
       .Text = "No Invoice" 
       .BackColor = Color.LightPink 
      End With 
     Else 
      With Main.VIEW_InvoiceReceivedDate 
       .Text = curr.Cells("Invoice Received").Value 
       .BackColor = Color.White 
      End With 

     End If 

     If IsDBNull(curr.Cells("Invoice Paid").Value) = True Then 
      With Main.VIEW_InvoicePaidDate 
       .Text = "Not Paid" 
       .BackColor = Color.LightPink 
      End With 
     Else 
      With Main.VIEW_InvoicePaidDate 
       .Text = curr.Cells("Invoice Paid").Value 
       .BackColor = Color.White 
      End With 

     End If 





    End Sub 

End Module 

我已經設置DataSourceUpdateMode.OnPropertyChanged和假設,這意味着在文本框的改變,它將更新數據源(即數據庫)。我猜這不是這種情況,因爲它不起作用。

我真正喜歡的是能夠在一個數據行編輯多個領域(通過文本框),然後點擊「保存更改」按鈕更新數據庫。

感謝

更新1

我已經做了一些更多的研究,以下的一些意見和答案,並寫了這個代碼我Save按鈕:

Public Sub Save() 
     Dim myCon = New OleDbConnection(My.Settings.Database_String) 


     myCon.Open() 

     Dim sqr = "UPDATE [Spares] SET [Name Of Person] = '" & Main.VIEW_NameOfPerson.Text & "', [SAP Job Number] = '" & CInt(Main.VIEW_SAPJobNo.Text) & "', " & _ 
       "[Site Name] = '" & Main.VIEW_Site.Text & "', [Asset Description] = '" & Main.VIEW_AssetDesc.Text & "', " & _ 
       "[Spares Supplier] = '" & Main.VIEW_SparesSupplier.Text & "', [Supplier Contact Name] = '" & Main.VIEW_SupplierContactName.Text & "', " & _ 
       "[Supplier Contact Phone Number] = '" & Main.VIEW_SupplierContactNumber.Text & "', " & _ 
       "[Supplier Contact Email] = '" & Main.VIEW_SupplierContactEmail.Text & "', [Spares Description] = '" & Main.VIEW_SparesDesc.Text & "', " & _ 
       "[Part Number] = '" & Main.VIEW_PartNumber.Text & "', [Quantity To Order] = '" & CInt(Main.VIEW_QuantityToOrder.Text) & "', " & _ 
       "[Cost] = '" & CDbl(Main.VIEW_CostEach.Text) & "', [Request Date] = '" & CDate(Main.VIEW_DateRequested.Text) & "' WHERE [ID] = '" & CInt(Main.VIEW_Ref.Text) & "'" 

     Dim Command = New OleDbCommand(sqr, myCon) 
     Command.ExecuteNonQuery() 
     myCon.Close() 
    End Sub 

Command.ExecuteNonQuery線試圖執行我得到指出以下錯誤:

類型的未處理的異常「System.Data.OleDb.OleDbException」 出現在system.data.dll

其他信息:數據類型不匹配 條件表達式中。

NB:是在sqr生成的字符串是:

UPDATE [Spares] SET [Name Of Person] = 'Name', [SAP Job Number] = '2', [Site Name] = 'Site', [Asset Description] = 'Asset', [Spares Supplier] = 'Spares Supplier', [Supplier Contact Name] = 'Contact Name', [Supplier Contact Phone Number] = 'Contact Email', [Supplier Contact Email] = 'Contact Number', [Spares Description] = 'Spare Desc', [Part Number] = 'Part Number', [Quantity To Order] = '1', [Cost] = '1', [Request Date] = '12/02/02' WHERE [ID] = '5'" 

我明明用假信息

我不能太遙遠,現在肯定!

+0

檢查出來:http://stackoverflow.com/questions/22436866/how-to-check-if- any-changes-were-made-in-datagridview/22437191#22437191 – equisde

+0

不幸的是,關鍵部分是保存更改,而你的答案是「做些保存更改」。這就是我需要的位 – SilverShotBee

+0

'Main.VIEW_Ref'究竟是什麼?看起來你可能有一個額外的球員參與。 – Plutonix

回答

0

我終於解決了用下面的代碼的問題,放在保存按鈕內:

Public Sub Save() 
     Dim myCon = New OleDbConnection(My.Settings.Database_String) 


     myCon.Open() 

     Dim sqr = "UPDATE [Spares] SET [Name Of Person] = """ & Main.VIEW_NameOfPerson.Text & """, [SAP Job Number] = " & CInt(Main.VIEW_SAPJobNo.Text) & ", " & _ 
       "[Site Name] = """ & Main.VIEW_Site.Text & """, [Asset Description] = """ & Main.VIEW_AssetDesc.Text & """, " & _ 
       "[Spares Supplier] = """ & Main.VIEW_SparesSupplier.Text & """, [Supplier Contact Name] = """ & Main.VIEW_SupplierContactName.Text & """, " & _ 
       "[Supplier Contact Phone Number] = """ & Main.VIEW_SupplierContactNumber.Text & """, " & _ 
       "[Supplier Contact Email] = """ & Main.VIEW_SupplierContactEmail.Text & """, [Spares Description] = """ & Main.VIEW_SparesDesc.Text & """, " & _ 
       "[Part Number] = """ & Main.VIEW_PartNumber.Text & """, [Quantity To Order] = " & CInt(Main.VIEW_QuantityToOrder.Text) & ", " & _ 
       "[Cost] = " & CDbl(Main.VIEW_CostEach.Text) & ", [Request Date] = " & CDate(Main.VIEW_DateRequested.Text) & " WHERE [ID] = " & CInt(Main.VIEW_Ref.Text) & "" 


     Dim Command = New OleDbCommand(sqr, myCon) 
     Command.ExecuteNonQuery() 
     myCon.Close() 
    End Sub 

End Module 
+1

這就是我建議你應該把代碼放到保存按鈕上。任何人都可以解決你的問題。 – Mahadev

+1

正如@Plutonix所提到的,我們可以輕而易舉地破解你的程序!使用存儲過程或至少在查詢中的參數化命令。並且嘗試在查詢中保存一個''',你應該逃避引號。 –

+0

@JeremyThompson它運行在本地筆記本電腦上,在商店裏......所以不,它沒有聯網到任何域 – SilverShotBee

0

如果用於綁定網格的DataTable是全局的,那麼在按鈕單擊時,您需要從要更新的DataTable中刪除記錄,然後使用相同的ID將編輯後的數據添加回DataTable中,然後重新綁定你的網格。這至少會更新你的視野。要更新數據庫,您需要調用Update查詢發送新數據的記錄ID。

如果你的數據表是不是全局設置視覺上不同的,因爲你通過在網格中每條記錄都循環再取出正確的,然後用相同的ID重新添加編輯的記錄。發送到數據庫調用仍然是一樣的。

希望這有助於讓你在正確的方向前進。

+0

感謝您的回答,我相信這有助於我進一步瞭解。我已經更新了我的問題,以反映 – SilverShotBee