【问题标题】:VB.NET How do I make calculation on some columns in datagridview from Form_Load eventVB.NET 如何从 Form_Load 事件对 datagridview 中的某些列进行计算
【发布时间】:2015-02-12 00:53:00
【问题描述】:

我是 vb.net 和论坛的新手,从中学到了很多,我在表单加载事件中自动将数据从 xml 文件导入到 datagridview,现在我需要一种方法来对某些列进行计算同样在表单加载事件中,我不想通过单元格更改或单元格结束编辑事件来做到这一点,就像我有这样的代码:

Private Sub DataGridView1_CellEndEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellEndEdit
    Try
        Dim order As DataGridView = DirectCast(sender, DataGridView)
        If IsDBNull(order(0, e.RowIndex).Value) Then Exit Sub
        order("column name", e.RowIndex).Value = order("column name", e.RowIndex).Value * textboxvalue.Text
    Catch ex As Exception
    End Try
End Sub

它工作正常,但我必须点击每个单元格并标签才能看到结果! 许多单元格点击不是好方法!

这样做的最佳方法是什么? 提前致谢

【问题讨论】:

  • 加载后,For/Loop怎么样? For Each row As DataGridViewRow In dgv.Rows
  • 谢谢 OneFineday ..你能给我更多的细节或代码
  • 我的答案不应该明确使用 - 您需要决定使用哪些单元格。另外,它对计算没有任何作用。你需要决定如何处理它。请记住,它们是基于 0 的。

标签: vb.net datagridview


【解决方案1】:

此示例将添加前两列。如果您不能保证它们是有效的整数,那么Integer.TryParse 会更好。您可以根据需要使用其他 DataType,例如 SingleDouble

For Each row As DataGridViewRow In dgv.Rows
  Dim total As Integer = Integer.Parse(row.Cells(0).Value) + Integer.Parse(row.Cells(1).Value)
Next

【讨论】:

  • 再次感谢您,不走运。有单元格是空值...显示错误。
  • 那你还没有填写DGV。
  • 不,这是在我但你的代码之后,并对单元格编号进行了一些修改......!!
  • 我已经向您展示了如何遍历每一行以及如何对这些值进行数学运算。确保数据在其中是你的工作 - 我不能从这里做到这一点。
  • 非常感谢我的朋友,我感谢你所做的一切。我只是一直在努力寻找解决方案。
【解决方案2】:

感谢 OneFineDay,我找到了解决方案:

For Each row As DataGridViewRow In DataGridView1.Rows
        row.Cells("column name").Value = row.Cells("column name").Value * textboxValue.Text
    Next

现在我可以通过插入其名称来计算所需的列值.. 再次感谢 OneFineDay 的帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-12-10
    • 1970-01-01
    • 1970-01-01
    • 2021-12-09
    • 1970-01-01
    • 2020-10-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多