if datagridview currentcell中的语句更改

时间:2013-04-04 06:32:38

标签: vb.net datagridview indexing row

当您移动到另一个单元格时,检查当前行的正确条件是什么。

我试试这段代码:

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的结果但是当我移动到另一个单元格,如果我在column1但是我移动了column2但是不同的行然后结果是changed

我希望你明白我的意思!

3 个答案:

答案 0 :(得分: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

答案 1 :(得分: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

答案 2 :(得分: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