VB.NET如何从Form_Load事件中对datagridview中的某些列进行计算

时间:2015-02-12 00:53:01

标签: vb.net datagridview

我是vb.net和论坛中的新手,我从中学到了很多东西,我在表单加载事件中自动将数据从xml文件导入到datagridview,现在我需要一种方法来计算一些列也在表单加载事件中,我不希望通过单元格更改或Cell End Edit事件来完成,就像我的代码一样:

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

它工作正常,但我必须单击每个单元格并标签出来查看结果! 许多细胞点击不好的方法!

这样做的最佳方式是什么? 在此先感谢

2 个答案:

答案 0 :(得分:1)

此示例将添加前两列。如果你不能保证它们是有效的整数,那么Integer.TryParse会更好。您可以根据需要使用其他数据类型,例如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

答案 1 :(得分:0)

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

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

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

相关问题