【问题标题】:datagridview cell null value errordatagridview 单元格空值错误
【发布时间】:2015-11-23 10:03:41
【问题描述】:

如果未输入 datagridview 单元格中的值,则目前处理错误的唯一方法是这样。有人有更好的解决方案吗?

Dim senderGrid = DirectCast(sender, DataGridView)

If TypeOf senderGrid.Columns(e.ColumnIndex) Is DataGridViewButtonColumn _
        AndAlso e.RowIndex >= 0 _ 
        AndAlso senderGrid.Columns(e.ColumnIndex).Name = "Add" Then

    Dim _status As Integer

    Try
        _status = Math.Abs(CInt(dgv6.Item("dgv6_Aktivno", dgv6.CurrentRow.Index).Value))
    Catch ex As Exception
        _status = 0
    End Try

    Dim _id As Integer
    Try
        _id = dgv6.Item("dgv6_id", dgv6.CurrentRow.Index).Value
    Catch ex As Exception
        _id = 0
    End Try

End if

【问题讨论】:

  • @Vivek.S 所以你想说如果值是空或者什么都没有然后写默认值?
  • 这个函数什么时候调用(哪个事件)??
  • @Vivek.S dgv6_CellContentClick
  • IF ELSE 比 Try Catch 更好

标签: vb.net datagridview


【解决方案1】:

当您将一个值转换为Integer 并将0 设置为其默认值时,您可以尝试Convert.ToInt32 处理null 值,但要注意一些预防措施。 Convert.ToInt32 将任何 null 值转换为 0

_status = Math.Abs(Convert.ToInt32("VALUE_TO_CONVERT"))

以及注意事项:

  1. VALUE_TO_CONVERT 必须是 String 类型。
  2. 您可能仍需要处理VALUE_TO_CONVERT 包含,.() 或字符等符号的情况。
  3. 您还需要注意VALUE_TO_CONVERT 的类型是否为Char

有关转换的详细讨论,请查看whats-the-main-difference-between-int-parse-and-convert-toint32MSDN for Convert.ToInt32

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-03-15
    • 2016-04-05
    • 1970-01-01
    • 2011-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多