0

I have a toolstripbutton that contains this code:

Dim total, tXS, tS, tM, tL, tXL As Integer

    For i = 0 To dvJOBranch.Rows.Count - 1
        tXS += dvJOBranch.Rows(i).Cells("XS").Value
        tS += dvJOBranch.Rows(i).Cells("S").Value
        tM += dvJOBranch.Rows(i).Cells("M").Value
        tL += dvJOBranch.Rows(i).Cells("L").Value
        tXL += dvJOBranch.Rows(i).Cells("XL").Value
    Next

    total = tXS + tS + tM + tL + tXL

    MsgBox(total)

Its only works once, like for example, if my datagridview cell contains all zeros, the total is zero, and then when I input numbers in the first row of the columns(for example I input 5), the total is zero again.

But if I put this code in a button, it works fine.

Thank you.

Matthew
  • 640
  • 3
  • 15
  • 38

1 Answers1

1

If you mean by total is a Label .. then you may put your code in your datagridview_cellvalidated event ..

Dim total as Integer

Private Sub dvJOBranch_CellValidated(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dvJOBranch.CellValidated

Dim tXS, tS, tM, tL, tXL As Integer

For i = 0 To dvJOBranch.Rows.Count - 1
    tXS += dvJOBranch.Rows(i).Cells("XS").Value
    tS += dvJOBranch.Rows(i).Cells("S").Value
    tM += dvJOBranch.Rows(i).Cells("M").Value
    tL += dvJOBranch.Rows(i).Cells("L").Value
    tXL += dvJOBranch.Rows(i).Cells("XL").Value
Next

total = tXS + tS + tM + tL + tXL

'MsgBox(total)

End Sub

In your ToolStripButton put the code Msgbox(total)

matzone
  • 5,703
  • 3
  • 17
  • 20
  • Hi sir, the TOTAL is the variable I use, not a label. Can you explain how can I put it in the cell validated, thank you – Matthew Sep 02 '13 at 06:43
  • Tnx for this sir, I wil try it. – Matthew Sep 02 '13 at 06:48
  • Hi sir, this is working for me, but every time I put value in the datagridview, its always popping out, I need to pop once. tnx – Matthew Sep 02 '13 at 06:51
  • I want to pop out when I click the toolstripbutton – Matthew Sep 02 '13 at 07:11
  • @Matthew .. Sorry I dont get it, if you want to pop up by clicking TSB, you original code has already worked isn't it ? .. you will need this code if you want always count the total after you input in specific column .. – matzone Sep 02 '13 at 07:15
  • Hi sir, my code is working but only in the first run, for example the total columns in the first run is 10, it will pop out 10, then if I change some values in the cell and the total is 20, it will pop out 10 again instead of 20. – Matthew Sep 02 '13 at 07:17
  • @Matthew .. It's updated .. Declare the total in Class form (not in the sub) – matzone Sep 02 '13 at 07:23