Click here to Skip to main content
15,887,027 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.
VB
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
Posted
Comments
Aydin Homay 16-Aug-13 6:11am    
you question same as this question:
http://www.codeproject.com/Questions/637772/DataGridView-Column-sum
look at here and check the solution 2

1 solution

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))
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900