【问题标题】:DataGridView loses data after failed validation验证失败后 DataGridView 丢失数据
【发布时间】:2018-03-11 08:54:41
【问题描述】:

我有一个 DataGridView 绑定到 Windows 窗体应用程序中的 Access 数据库。当我创建一个新行并输入无效数据(根据 Access 中设置的规则进行验证,例如单元格不能为空),然后移动到另一行时,数据将丢失。 DataGridView 行没有抛出错误或错误信息。

我在 CellValidating 和 RowValidating 事件上尝试了 e.cancel,但没有成功。有什么想法吗?

【问题讨论】:

    标签: datagridview


    【解决方案1】:

    当系统抛出异常时会发生这种情况,例如“NULL 值在现在允许的任何列中都不允许,重复主键等。”

    为了处理和观察这一点,您需要处理 DataGrid 视图的“DataError”事件。在那里您可以使用 e.Cancel=True ,它将在用户正在使用的数据行上显示“错误图标”。

    Private Sub ctlBrokingIntBrokerageList_DataError(DataGridView As DataGridView, e As DataGridViewDataErrorEventArgs) Handles Me.DataError
    
               If TypeOf (e.Exception) Is FormatException Then
                    e.Cancel = True
    
                    If dgvMain.CurrentRow IsNot Nothing Then
                        dgvMain.CurrentRow.ErrorText = e.Message
                    End If
                End if 
    End sub
    

    【讨论】:

      猜你喜欢
      • 2013-06-30
      • 1970-01-01
      • 1970-01-01
      • 2022-12-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多