2012-05-17 21 views
0

我們有3列一個DataGridView: InvoicedOn,PaidOn及金額找出一個DataGridView的當前行的特定列的值

在MouseDown事件是這行代碼需要被固定:

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

我們試圖找出curent行的Amount列中的值。

我們還收到此錯誤信息:

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

你能告訴我如何解決這個MSGBOX所以它會顯示出價值?

回答

1

語法DataGridViewPayments.Item("Amount", DataGridViewPayments.CurrentRow.Index)將在列的交叉點名爲「金額」得到的DataGridViewCell與當前行的索引(假設它存在)(假設該CurrentRow是沒什麼)。

爲了得到該小區的需要引用命名Value

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

Amount柱建議的數值是目前的DataGridViewCell的財產價值,但是,請記住,如果當前單元格是空的或空的,你可能會得到一個異常。在這種情況下最好是:

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 
+0

感謝您的有用代碼和快速回復。我會用它。 –

1

使用列索引,而不是列名

MsgBox(DataGridViewPayments.Item(2, DataGridViewPayments.CurrentRow.Index)).value, "") 
相關問題