Click here to Skip to main content
14,327,148 members
Rate this:
Please Sign up or sign in to vote.
See more:

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

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

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

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

    End Sub
Aydin Homay 16-Aug-13 6:11am
you question same as this question:
look at here and check the solution 2

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

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))

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100