0

I'd like to perform simple calculations using CellEndEdit event. I have the following code:

Private Sub DataGridView2_CellEndEdit(ByVal sender As Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView2.CellEndEdit

    Dim Total As DataGridView = DirectCast(sender, DataGridView)

    If IsDBNull(Total(0, e.RowIndex).Value) Then Exit Sub

    Total("C3", e.RowIndex).Value = Total("C1", e.RowIndex).Value + Total("C2", e.RowIndex).Value

End Sub

But when I entered 20 into both Column C1 and C2, the C3 is showing 2020 instead of 40. Please help me out with this.

1
  • Thank you guys so much for all your answers. Feb 22, 2013 at 6:29

2 Answers 2

1

Well, if the Type of Total("C1", e.RowIndex).Value is string, it's gonna concatenate them. Try

Total("C3", e.RowIndex).Value = (((cint)Total("C1", e.RowIndex).Value) + ((cint(Total("C2", e.RowIndex).Value)).ToString()

To do an addition.

HTH

1

As you are dealing with strings the + operator is being used as a concat... Try;

Total("C3", e.RowIndex).Value = CInt(Total("C1", e.RowIndex).Value) + CInt(Total("C2", e.RowIndex))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.