vb.netdatagridviewtoolstripbutton

toolstripbutton error in vb.net


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.


Solution

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