2013-04-04 44 views
1

當您移動到另一個單元格時,檢查當前行是否發生更改的正確條件是什麼?if datagridview currentcell change中的語句

我試試這個代碼:

Private Sub Legal_RecordsDataGridView_CurrentCellChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Legal_RecordsDataGridView.CurrentCellChanged 

    if Legal_RecordsDataGridView.CurrentRow.Index = Legal_RecordsDataGridView.CurrentCellAddress.Y then 

     msgbox("changed") 
     else 
     msgbox("no") 

    endif 
End Sub 

我總是得到「不」,因爲他們總是有相同的索引結果。

如何檢查CurrentRow索引是否相同,當我移動到另一個單元格時,例如,當我移動到另一個單元格但同一行時,我想要no的結果,但是當我移動到另一個單元格時如果我在列1但我移動到列2但不同的行然後結果是changed

我希望你明白我的意思!

回答

1

嘗試CurrenCellChanged事件..

例如後獲得行索引..

Private Sub Legal_RecordsDataGridView_CurrentCellChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Legal_RecordsDataGridView.CurrentCellChanged 

'Add the condition here.. 
'Say, what's the value of last row index? If you don't have a value for last row index, then exit sub 


'get here the last row index (set the variable), this should give you the row index that you can use in your next cell change event.. 
mynewrowindex = Legal_RecordsDataGridView.currentrow.index 

End Sub 
0

試試這個代碼

mRow_Leave是一個全局變量

Dim mRow_Leave as integer 


Private Sub DataGridView1_CurrentCellChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DataGridView1.CurrentCellChanged 

     If mRow_Leave = DataGridView1.CurrentCell.RowIndex Then 

      MsgBox("no") 
     Else 
      MsgBox("change") 
     End If 
     mRow_Leave = DataGridView1.CurrentCell.RowIndex 
    End Sub 

Private Sub DataGridView1_RowLeave(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.RowLeave 

    mRow_Leave = e.RowIndex 

End Sub 
+0

謝謝你的工作! – 2013-04-05 07:09:31

0
Private Sub Legal_RecordsDataGridView_CurrentCellChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Legal_RecordsDataGridView.CurrentCellChanged 

    if Legal_RecordsDataGridView.cells.Index = Legal_RecordsDataGridView.CurrentCellAddress.Y then 

     msgbox("changed") 
     else 
     msgbox("no") 

    endif 
End Sub