我正在构建一个表单,我需要以编程方式设置绑定和数据集(因为它是变量,可以加载不同的文档)。我已设法将表单加载到DataGridView
并将DataGridView
中的信息加载到某些TextBox中进行结构化编辑:
但是我很难将编辑后的信息保存回数据库。它甚至不会更新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 :(得分:0)
如果用于绑定网格的DataTable是全局的,那么在按钮单击时,您需要从想要更新的DataTable中删除记录,然后使用相同的ID将编辑后的数据添加回DataTable然后重新绑定你的网格。这至少会更新您的视野。要更新数据库,您需要调用Update
查询,向新数据发送记录ID。
如果您的DataTable不是全局的,那么设置视觉是不同的,因为您必须遍历网格中的每个记录然后删除正确的记录,然后再次使用相同的ID添加已编辑的记录。发送到数据库的调用仍然是相同的。
希望这有助于您朝着正确的方向前进。
答案 1 :(得分: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