0

我们有一个包含 3 列的 DataGridView:InvoicedOn、PaidOn 和 Amount

在 MouseDown 事件上是需要修复的这行代码:

MsgBox(DataGridViewPayments.Item("Amount", DataGridViewPayments.CurrentRow.Index), "")

我们试图找出当前行的 Amount 列中的值。

我们还收到此错误消息:

Conversion from string "" to type 'Integer' is not valid.

你能告诉我如何修复 MsgBox 以便它显示值吗?

4

2 回答 2

1

使用列索引而不是列名

MsgBox(DataGridViewPayments.Item(2, DataGridViewPayments.CurrentRow.Index)).value, "")
于 2012-05-17T23:37:25.877 回答
1

该语法DataGridViewPayments.Item("Amount", DataGridViewPayments.CurrentRow.Index)将在名为“Amount”的列(假设它存在)与当前行索引(假设 CurrentRow 不是 Nothing)的交叉处获取DataGridViewCell 。

要获取该单元格的值,您需要引用名为 DataGridViewCell 的属性Value

DataGridViewPayments.Item("Amount", DataGridViewPayments.CurrentRow.Index).Value

Amount列表明存在一个数值,但请记住,如果当前单元格为空或 null,您可能会遇到异常。在这种情况下,最好这样做:

object o = DataGridViewPayments.Item("Amount", DataGridViewPayments.CurrentRow.Index).Value
if o Is Nothing OrElse o.ToString = "" then 
  MsgBox("The cell is empty or null", "Message Title")
else
  MsgBox("The cell value is " + o.ToString, "Message Title")
end if
于 2012-05-17T23:40:50.913 回答