我的基于DataGridView的应用程序有问题。将记录添加到网格后,如果我随后尝试更新或删除新记录,则会出现以下相应错误。 并发冲突:UpdateCommand影响了预期的1条记录中的0条。 并发冲突:DeleteCommand影响了预期的1条记录中的0条。 在RowValidated事件中强制保存数据集。
Private Sub uxGrid_RowValidated(sender As Object, e As DataGridViewCellEventArgs) Handles uxGrid.RowValidated
If IsDGVRowDirty(sender, e.RowIndex) Then
Save()
End If
End Sub
Private Sub Save()
Const procName As String = "Save"
Try
_Logger.SendLog(Me.Name & "." & procName & " - Saving alarm definition data.", NLog.LogLevel.Trace)
_myAlarmDefinitionMngr.Save(_myDataSet)
_Logger.SendLog(Me.Name & "." & procName & " - Alarm definition data has been saved.", NLog.LogLevel.Trace)
Catch ex As Exception
MsgBox("There was a problem whilst saving your changes. Please reload the form and try again. " & vbCrLf & vbCrLf & ex.Message, MsgBoxStyle.Critical)
_Logger.SendLog(ex.Message & ". Thrown in module " & Me.Name.ToString & "." & procName, NLog.LogLevel.Error, ex)
Finally
End Try
End Sub
客户端应用程序位于一个相对简单的3层架构设计之上,与Oracle 11后端通信,在这种情况下包含元素AlarmDefinitionManager.vb,AlarmDefinitionDB.vb,AlarmDefinition.vb。
Public Function Save(ByVal myDataSet As DataSet) As Integer
Dim myOda As OracleDataAdapter
Dim myConnection As New OracleConnection
Dim myCommand As OracleCommand = Nothing
Dim myDataAdapter As OracleDataAdapter
Dim myBuilder As OracleCommandBuilder
Dim sqlStatement As String
myConnection = New OracleConnection
myConnection.ConnectionString = _connectStr
sqlStatement = "SELECT ID, LEGENDID, STATUSID, STATUSTYPEID, DIGITALSET FROM P_TBL_ALARMDEF"
If myDataSet.HasChanges Then
Try
myOda = New OracleDataAdapter(sqlStatement, myConnection)
myBuilder = New OracleCommandBuilder(myOda)
myOda.SelectCommand = New OracleCommand(sqlStatement, myConnection)
myOda.Update(myDataSet, "AlarmDefinition")
myDataSet.AcceptChanges()
myConnection.Close()
Catch ex As Exception
Throw
Finally
myCommand = Nothing
myDataAdapter = Nothing
myBuilder = Nothing
CType(myConnection, IDisposable).Dispose()
End Try
End If
End Function
我对情况的理解是我需要刷新网格中的数据源,以便它反映所做的更改。为了实现这一点,我希望通过将刷新过程挂钩到datagridview中的事件来刷新网格。然而,我尝试过的事件似乎都没有奏效。到目前为止尝试了每个事件,我收到的错误是,不允许在事件中刷新数据源。经过反思,这看似合乎逻辑是否有与datagridview关联的事件可用于强制刷新数据源?
答案 0 :(得分:1)
在验证行时,您不应尝试保存数据集。 对数据集进行更改,然后将数据源设置为null,并将数据源重新连接回网格以强制刷新。
我个人不喜欢对数据网格进行更改。我更喜欢编辑datagrid外的行,并向SQL发送UPDATE cmd,然后刷新我的网格。我可以更好地控制对网格所做的更改。
答案 1 :(得分:0)
排序我的问题。每行更改都直接保存到RowValidated事件中的数据库中。