【问题标题】:Insert data to a datagridview manually with an added column使用添加的列手动将数据插入到 datagridview
【发布时间】:2014-12-04 04:51:21
【问题描述】:

我想用这个在我的 datagridview 中插入值:

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

但是有这个错误:“Argument 'Expression' cannot be convert to type '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

【问题讨论】:

  • @nempoBu4 仍然有同样的错误

标签: vb.net datagridview


【解决方案1】:

就这样试试吧

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

顺便说一句,你并没有真正用这种方式格式化任何东西......你也可以这样写

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

结果是一样的。

别忘了检查值是DBNULL.Value 还是0

【讨论】:

  • 谢谢。我也做了同样的事情。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-10-23
  • 2013-10-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多