如何对DataGrid单元格中插入的值求和? [英] How to sum values inserted in DataGrid cells?

查看:72
本文介绍了如何对DataGrid单元格中插入的值求和?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我想在DataGridView的两个单元格中插入两个值。



这应该在运行期间工作,用户在DGV的特定单元格中输入2个值。



输入这些值后,该程序应自动计算并输出另一个特定单元格中的结果。



我已编写代码但我的代码不能像它一样工作应该工作。



我把代码放在DGV cellContentClick下,这可能是错的,因为我不知道在哪里放代码。



我的代码出了什么问题?请帮忙。

Hi,

I want to sum two values inserted in two cells in a DataGridView.

This should work during run-time, user enters 2 values in specific cells in DGV.

When these values are entered, this program should automatically compute and output the result in another specific cell.

I already wrote a code but my code doesn't work like it supposed to work.

I put the code under DGV cellContentClick, this may be wrong, because I dont know where to put the code.

What is wrong with my code? Pls help.

Private Sub dgdTax_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgdTax.CellContentClick

        '======================================== CALCULATION ==============================================='

        'to calculate r4rm1
        Dim r2rm1 As Double
        Dim r3rm1 As Double

        Try
            r2rm1 = CDbl(dgdTax(1, 2).Value)
        Catch e1 As Exception
            MessageBox.Show(e1.Message)
            Exit Sub
        End Try

        Try
            r3rm1 = CDbl(dgdTax(1, 3).Value)
        Catch e2 As Exception
            MessageBox.Show(e2.Message)
            Exit Sub
        End Try

        dgdTax(1, 4).Value = r2rm1 + r3rm1

    End Sub

推荐答案

有两个代码中的问题。以下是解决方案。



1.使用CellValueChanged事件

2.更改总单元格后调用DataGridView.InvalidateCell()方法。否则你无法立即看到更改。



dgdTax.InvalidateCell(dgdTax(1,4))
There are two issues in your code. Following are the solutions.

1. Use CellValueChanged event
2. Call DataGridView.InvalidateCell() method after changing the total cell. Otherwise you cant see the change immediately.

dgdTax.InvalidateCell(dgdTax(1, 4))


这篇关于如何对DataGrid单元格中插入的值求和?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆