如果我正确阅读了您的问题,则您要查找的值是 222-225,并且该值位于 Dgv 的指定单元格中(我猜是 DataGridView)。如果我的理解是正确的,那么有几件事正在发生。
首先,我不确定您为什么要尝试使用以下代码行将该值转换为双精度:
a = CDbl(Dgv.Item(8, Dgv.CurrentRow.Index).Value)
DataGridView的Item
属性包含一个DataGridViewCell
,而返回的Value
属性是一个。我相信,尝试将 222-225 转换为双精度会失败(尽管由于这是 VB.NET,因此它可能不会取决于您设置的选项 - 我对 VB.NET 并不熟悉使用 C#)。DataGridViewCell
Object
即使它确实成功地工作(我不确定输出会是什么),也Split
需要一个字符串。我会将该行代码更改为以下内容:
a = Dgv.Item(8, Dgv.CurrentRow.Index).Value.ToString()
现在你有了一个可以使用 Split 的字符串。您发布的代码中的 Split 似乎是 Visual Basic (pre-.NET) Split 方法Split Function (Visual Basic)。正如其他人所提到的,Split 根据分隔符返回一个字符串数组。在您的代码中,您没有将 Split 的结果分配给任何东西,因此您无法获取这些值。
我建议使用 .NET 版本的 Split ( String.Split Method ) - 您可以调用多种方法String.Split
,但出于代码的目的,我会像这样使用它:
Dim splits As String() = a.Split(New Char() { "-" })
其中 a 是上面选定的 DataGridViewCell 中的字符串值。这将为您提供一个 2 元素数组:
splits(0) = "222"
splits(1) = "225"
最后一部分是你的公式。由于您有字符串,因此您需要将它们转换为数字数据类型:
total = (CDbl(splits(1)) - CDbl(splits(0))) + 1
变为 (225 - 222) + 1 = 4。
总而言之,它看起来像这样:
Dim a As String
Dim total As Double
Dim splits() As String
a = Dgv.Item(8, Dgv.CurrentRow.Index).Value.ToString()
splits = a.Split(New Char() { "-" })
total = (CDbl(splits(1)) - CDbl(splits(0))) + 1
Dgv.Item(9, Dgv.CurrentRow.Index).Value = total