我有一个包含此代码的工具条按钮:
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)
它只能工作一次,例如,如果我的datagridview单元格包含全零,则总数为零,然后当我在列的第一行输入数字时(例如我输入5),总数为零试。
但是如果我把这个代码放在一个按钮中,它就能正常工作。
谢谢。
答案 0 :(得分:1)
如果您的意思是total
是标签..那么您可以将代码放入datagridview_cellvalidated
事件中。
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
在ToolStripButton中输入代码Msgbox(total)