You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

VB.NET中Dapper+Access数据库触发Value cannot be null错误求助

问题解决:刷新Grid数据后删除行触发"Value cannot be null"错误

错误原因

从报错堆栈来看,问题出在Kimtoo.BindingProvider.DGVBindingProvider内部的字典操作——删除事件触发时,用于关联数据行的key为null,导致字典查找失败。这是因为你只在Form加载时设置了OnDelete委托,刷新数据重新Bind后,旧委托与新绑定的数据源上下文不匹配,BindingProvider无法找到对应的数据行key;同时Grid.ReadOnly = True并未彻底禁用删除操作的触发逻辑。

解决方案

方案1:每次Bind数据后重新设置OnDelete委托

刷新数据后,新数据源会替换旧数据,此时需要重新绑定OnDelete委托,确保它关联当前的数据源上下文:

Private Sub LoadData()
    Dim user = uService.GetUser()
    Grid.Bind(user)
    ' 绑定新数据后重新设置OnDelete
    Grid.OnDelete(Of User)(Function(r, row)
                               Return False
                           End Function)
End Sub

方案2:直接禁用Grid的删除功能(更彻底)

既然不需要删除行,直接关闭Grid的删除权限,从根源上避免触发删除事件:

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Grid.ReadOnly = True
    Grid.AllowUserToDeleteRows = False ' 新增这行,直接禁用删除操作
    LoadData()
End Sub

方案3:确保User类有非空主键

Kimtoo的BindingProvider通常依赖数据模型的主键作为字典key,如果User类未定义主键,或者查询返回的主键值为null,也会触发该错误。请确保User类包含非空的主键属性:

Public Class User
    Public Property Id As Integer ' 主键字段,必须保证非空
    Public Property Username As String
    ' 其他业务属性
End Class

内容的提问来源于stack exchange,提问作者roy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.20 22:10:28