2016-09-05 37 views
1

Cell enter - Operation is not valid because it results in a reentrant call to the SetCurrentCellAddressCore function如果我這樣做的錯誤會出現這樣的:單元格中輸入 - 操作是無效的,因爲它會導致重入調用SetCurrentCellAddressCore功能

Private Sub dg1_CellEnter(sender As Object, e As DataGridViewCellEventArgs) Handles dg1.CellEnter 
if e.columnindex = 4 
    if dg1.CurrentRow.Cells("id").Value = 0 
    dg1.endEdit(true) 
    msgbox("choose any item in cell3 before you can proceed here") 
    dg1.CurrentCell = dg1.CurrentRow.Cells(3) 
    exit sub 
    end 
    'my code here . . . 
end if 
End Sub 

我只是想阻止用戶進入或執行我的代碼在cell 4如果她/他 沒有選擇在cell 3

任何物品。當用戶選擇cell 3cells("id")將有一個價值取決於cell 3值的id的項目。但是當我點擊cell 4時總會出現錯誤。

任何人都可以解決這個問題?在此先感謝

回答

1

您得到可重入異常的原因是設置當前單元格會強制觸發CellEnter事件。然後

Delegate Sub SetColumnIndex(ByVal i As Integer)

Private Sub Mymethod(ByVal columnIndex As Integer) 
    dg1.CurrentCell = dg1.CurrentRow.Cells(columnIndex) 
End Sub 

在你的代碼:爲了避免此異常,請加入到這個代碼

if dg1.CurrentRow.Cells("id").Value = 0 
    msgbox("choose any item in cell3 before you can proceed here") 
    Dim method As New SetColumnIndex(AddressOf Mymethod) 
    dg1.BeginInvoke(method, 3) 
    Exit Sub 
end if 

如果這不起作用只是通知我。

相關問題