我正在開發一個應用程式,它顯示我在服務器上的一個表,我希望用戶直接在應用程式中更新它(插入/洗掉/更新)。
我創建了一個RadGridView名為definitionViewTelerik 來顯示資料,我按照 Telerik 檔案中直接給出的說明進行操作:使用 ADO.NET 更新資料庫
但是我堅持第一部分:更改當前行時更新資料庫
我試圖使示例中顯示的代碼適應我的代碼,但我不斷收到adapter.Update()陳述句錯誤。
Public Class definitionSolution
Private lastEditRow As DataRow = Nothing
Private bindingSource
Public Sub New()
InitializeComponent()
Dim bindingSource As New BindingSource
Dim dsSolution As New DataSet()
adapter.Fill(dsSolution, "MyTable")
bindingSource.DataSource = dsSolution.Tables("MyTable")
definitionView.DataSource = bindingSource
AddHandler bindingSource.CurrentChanged, AddressOf DefinitionView_CurrentChanged
End Sub
Private Sub AutoSavingDataBSEventsForm_Load(ByVal sender As Object, ByVal e As EventArgs)
' TODO: This line of code loads data into the 'nwindDataSet.Employees' table. You can move, or remove it, as needed.
Dim dsSolution As New DataSet()
adapter.Fill(dsSolution, "MyTable")
bindingSource.DataSource = dsSolution.Tables("MyTable")
Dim current As Object = bindingSource.Current
If current IsNot Nothing Then
Me.lastEditRow = (CType(current, DataRowView)).Row
End If
End Sub
Private Sub DefinitionView_CurrentChanged(ByVal sender As Object, ByVal e As EventArgs)
Dim dataRow As DataRow = CType(CType(sender, BindingSource).Current, DataRowView).Row
If lastEditRow IsNot Nothing AndAlso lastEditRow.RowState = DataRowState.Modified Then
adapter.Update(lastEditRow)
End If
lastEditRow = dataRow
End Sub
End Class
SQLDataAdapter 在另一個模塊中初始化如下:
Public queryString = "SELECT * FROM [dbo].Mytable"
Public adapter As New SqlDataAdapter(queryString, DB_CONNECTION)
錯誤如下:
Error BC30518 Overload resolution failed because no accessible 'Update' can be called with these arguments:
- Public Overrides Function Update(dataSet As DataSet) As Integer': Value of type 'DataRow' cannot be converted to 'DataSet
- Public Overloads Function Update(dataRows As DataRow()) As Integer': Value of type 'DataRow' cannot be converted to 'DataRow()
- Public Overloads Function Update(dataTable As DataTable) As Integer': Value of type 'DataRow' cannot be converted to 'DataTable
有人知道如何解決這個問題嗎?
同樣來自他們的檔案:
假設我們有一個 ADO.NET DataTable,它從 SqlDataAdapter 加載其資料,并且系結到 BindingSource 組件。此外,BindingSource 組件系結到 RadGridView 控制元件。
我知道我有一個來自 SqlDataAdapter 的資料集,它系結到一個 BindingSource 組件,但我錯過了最后一點。將 BindingSource 系結到 RadGridView 控制元件是什么意思?
在@jmcilhinney 評論后編輯
從切換adapter.Update(lastEditRow)到adapter.Update(dsSolution)給我以下錯誤:
System.Reflection.AmbiguousMatchException: 'Overload resolution failed because no Public 'Update' is most specific for these arguments:
'Public Overrides Function Update(dataSet As System.Data.DataSet) As Integer':
Not most specific.
'Public Function Update(dataRows As System.Data.DataRow()) As Integer':
Not most specific.
'Public Function Update(dataTable As System.Data.DataTable) As Integer':
Not most specific.'
從切換adapter.Update(lastEditRow)到adapter.Update(lastEditRow.ItemArray)給我以下錯誤:
System.InvalidCastException: 'Unable to cast object of type 'System.Object[]' to type 'System.Data.DataRow[]'
uj5u.com熱心網友回復:
您的資料配接器的Update方法接受多行并根據需要執行插入、更新和洗掉。該Update方法已多載,它將接受一個DataRow陣列,因此您可以將單行放入一個陣列并傳遞:
adapter.Update({lastEditRow})
否則,您需要傳遞整體DataTable并讓它挑選出需要保存的行。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/520128.html
上一篇:將列舉值存盤到xml檔案
下一篇:WCFcustomUserNamePasswordValidatorType錯誤-無法加載檔案或程式集“CustomUserNameValidator”或其依賴項之一
