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

VB.NET中DataGridView复选框列:库存为0时禁止勾选并弹窗提示

优化实现方案:禁止库存为0的行勾选DataGridView复选框

问题分析

你的现有代码在CellEndEdit事件中处理逻辑,但存在几个可优化的点:

  • 声明的变量selected未实际使用,可删除
  • 未处理库存单元格值为DBNull的情况,可能引发类型转换异常
  • 当前逻辑是勾选后再取消,用户体验不够流畅,可提前拦截勾选操作

推荐实现方式

推荐结合CellContentClick(点击复选框时立即触发)和CellValueChanged(处理代码修改复选框值的场景)事件,实现更严谨的校验逻辑:

Private Sub dgv_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles dgv.CellContentClick
    ' 仅处理复选框列的点击操作
    If e.ColumnIndex = dgv.Columns("checkbox").Index AndAlso e.RowIndex >= 0 Then
        Dim stockCellValue = dgv.Rows(e.RowIndex).Cells(9).Value
        ' 校验库存为空或库存值≤0的情况
        If stockCellValue Is DBNull.Value OrElse Convert.ToInt32(stockCellValue) <= 0 Then
            ' 强制取消勾选
            dgv.Rows(e.RowIndex).Cells("checkbox").Value = False
            ' 弹出提示框
            MessageBox.Show("库存为0,无法勾选该行", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning)
            ' 阻止后续编辑操作,避免重复触发
            dgv.CancelEdit()
        End If
    End If
End Sub

' 处理通过代码修改复选框值的场景
Private Sub dgv_CellValueChanged(sender As Object, e As DataGridViewCellEventArgs) Handles dgv.CellValueChanged
    If e.ColumnIndex = dgv.Columns("checkbox").Index AndAlso e.RowIndex >= 0 Then
        Dim isChecked = Convert.ToBoolean(dgv.Rows(e.RowIndex).Cells("checkbox").Value)
        If isChecked Then
            Dim stockCellValue = dgv.Rows(e.RowIndex).Cells(9).Value
            If stockCellValue Is DBNull.Value OrElse Convert.ToInt32(stockCellValue) <= 0 Then
                ' 临时移除事件避免递归触发
                RemoveHandler dgv.CellValueChanged, AddressOf dgv_CellValueChanged
                dgv.Rows(e.RowIndex).Cells("checkbox").Value = False
                AddHandler dgv.CellValueChanged, AddressOf dgv_CellValueChanged
                MessageBox.Show("库存为0,无法勾选该行", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning)
            End If
        End If
    End If
End Sub

代码说明

  • CellContentClick事件:用户点击复选框时立即校验库存,避免勾选后再取消的突兀感
  • 增加DBNull值判断:防止库存单元格为空时引发转换异常
  • CellValueChanged事件:覆盖通过代码修改复选框值的场景,同时通过临时移除事件避免递归触发
  • 替换MsgBox为MessageBox.Show:支持自定义提示标题和图标,更符合现代UI规范

原代码优化版本

如果想基于你的原代码调整,可做如下修改:

Private Sub dgv_CellEndEdit(sender As Object, e As DataGridViewCellEventArgs) Handles dgv.CellEndEdit
    ' 仅处理复选框列的编辑结束事件
    If e.ColumnIndex = dgv.Columns("checkbox").Index AndAlso e.RowIndex >= 0 Then
        Dim isChecked = Convert.ToBoolean(dgv.Rows(e.RowIndex).Cells("checkbox").Value)
        If isChecked Then
            Dim stockCellValue = dgv.Rows(e.RowIndex).Cells(9).Value
            ' 处理空值和库存≤0的情况
            If stockCellValue Is DBNull.Value OrElse Convert.ToInt32(stockCellValue) <= 0 Then
                dgv.Rows(e.RowIndex).Cells("checkbox").Value = False
                MessageBox.Show("库存为0,无法勾选该行", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning)
            End If
        End If
    End If
End Sub

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.26 22:42:19