2010-07-20 25 views

回答

4

有一個RowValidated事件將適用於此。然後,您可以將值從網格中拉出並填充數據庫命令。

1

以下爲我工作得很好:

' ListChanged event is called whenever binding or datatable changes 
Private Sub ProjectBindingSource_ListChanged(sender As Object, e As ListChangedEventArgs) Handles ProjectBindingSource.ListChanged 

    Select Case e.ListChangedType 
     ' Only update when listchanges are of following types 
     Case ListChangedType.ItemChanged, ListChangedType.ItemAdded, ListChangedType.ItemDeleted 
      Try 
       'Update method will select the required update/insert/delete sql according to the tableadapter settings, 
       'after successful update, it will accept changes in the dataset and change status of all rows back to unchanged 
       Me.ProjectTableAdapter.Update(Me.StuklijstDataSet.Project) 

      Catch ex As Exception 
       ' exceptions will bounce back from the server, 
       ' this will handle all possible data problems: concurrency, foreign key constraints, null not allowed, ... ; 
       ' the update method (see above) will also mark the row with an ErrorProvider icon and tooltip with the error message 

       ' if update is unsuccessful, roll back changes to the datatable 
       Me.StuklijstDataSet.Project.RejectChanges() 

       ' resetbindings to sort out problems with the control when rolling back adding or deleting records 
       Me.ProjectBindingSource.ResetBindings(False) 
      End Try 

    End Select 

End Sub 

時,只有在數據表和Listchangedtype的檢查負載時提高3-4倍是非常快的( < 1ms)。 綁定源的RaiseListChangedEvents屬性可以設置爲False臨時關閉ListChanged事件,但這似乎會導致填充數據表時出現問題。我相信,不需要打開/關閉事件。

來自服務器的錯誤消息不是非常用戶友好的。 上述方法可以通過將異常作爲SqlException對象讀取,檢索SQL錯誤代碼並將ErrorProvider消息更改爲更加簡潔的文本來擴展。

相關問題