DataGridView1_CellValueChanged 死循环了 小弟我的代码该如何修改
DataGridView1_CellValueChanged 死循环了 我的代码该怎么修改?
Private Sub DataGridView1_CellValueChanged(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellValueChanged
ariginalColumn = DataGridView1.CurrentCell.RowIndex
ariginalRow = DataGridView1.CurrentCell.ColumnIndex
ifCellChanged = True
'循环计算剩余数量()
For i = 0 To DataGridView1.Rows.Count - 2
Dim a As Double
Dim b As Double
a = DataGridView1.Rows(i).Cells(6).Value
b = DataGridView1.Rows(i).Cells(7).Value
If Not DataGridView1.Rows(i).Cells(6).Value Is DBNull.Value Or DataGridView1.Rows(i).Cells(7).Value Is DBNull.Value Then
DataGridView1.Rows(i).Cells(8).Value = CStr(Val(a) - Val(b))
End If
Next
End Sub
------解决方案--------------------
跟什么Flag没有关系。因为DataGridView1_CellValueChanged被级联触发,完全是可能事情,你不能想当然地弄一个Flag来回避正常的级联触发事件。
其实这是一个模式,就是赋值之前你先要检查一下,当两个值不同时才赋值。自然就不会出现死循环。
Private Sub DataGridView1_CellValueChanged(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellValueChanged
ariginalColumn = DataGridView1.CurrentCell.RowIndex
ariginalRow = DataGridView1.CurrentCell.ColumnIndex
ifCellChanged = True
'循环计算剩余数量()
For i = 0 To DataGridView1.Rows.Count - 2
Dim a As Double
Dim b As Double
a = DataGridView1.Rows(i).Cells(6).Value
b = DataGridView1.Rows(i).Cells(7).Value
If Not DataGridView1.Rows(i).Cells(6).Value Is DBNull.Value Or DataGridView1.Rows(i).Cells(7).Value Is DBNull.Value Then
DataGridView1.Rows(i).Cells(8).Value = CStr(Val(a) - Val(b))
End If
Next
End Sub
------解决方案--------------------
跟什么Flag没有关系。因为DataGridView1_CellValueChanged被级联触发,完全是可能事情,你不能想当然地弄一个Flag来回避正常的级联触发事件。
其实这是一个模式,就是赋值之前你先要检查一下,当两个值不同时才赋值。自然就不会出现死循环。