2014-12-04 33 views
0

我想用這個來插入我的datagridview裏面的值:插入數據手動一個DataGridView與添加的列

For Each rows As DataGridViewRow In dgView.Rows 
    rows.Cells("Ave").Value = Format(Val(dgView.Rows(0).Cells("Qty Issued"))/countMonths).ToString 
Next 

但有此錯誤:「論證‘表達’不能轉換爲類型‘DataGridViewTextBoxCell’ 「。

這是場景:
我得到了一個datagridview由數據庫中的數據填充。我以編程方式添加了列名「Ave」,然後我想要獲得列的平均值並將其放置到使用循環添加的列「Ave」。我很樂意接受建議。

感謝所有幫助過我的人,我在一天左右的時間裏發現了它。這是新的代碼,我用:

For Each rows As DataGridViewRow In dgView.Rows 
      ave = rows.Cells("Qty Issued").Value/countMonths 
      rows.Cells("Ave").Value = Format(ave, "0.00") 
Next 
+0

@ nempoBu4仍然有同樣的錯誤 – johnprima 2014-12-04 05:10:48

回答

0

只是嘗試這樣

rows.Cells("Ave").Value = Format((dgView.Rows(0).Cells("Qty Issued")).Value/countMonths).ToString 

,但你應該知道,你總是採取的第一行"Qty Issued"

,如果它是一排按行平均我會建議使用

rows.Cells("Ave").Value = Format((rows.Cells("Qty Issued")).Value/countMonths).ToString 

和btw你都沒有真正格式化這樣...你也可以寫這個

rows.Cells("Ave").Value = dgView.Rows(0).Cells("Qty Issued").Value/countMonths 

結果是一樣的。

,不要忘記檢查,如果值是DBNULL.Value0

+0

感謝。我有點做了同樣的事情。 – johnprima 2014-12-05 01:46:07

相關問題