2013-07-07 43 views
2

我使用了CellEndEdit事件,在編輯單元格值後,我按下Enter鍵,然後單元格焦點向下移動。DataGridView SetFocus在CellEndEdit後

我想重點回到我編輯值的原始單元格。

我用了很多方法,但都失敗了。

Private Sub DataGridVie1_CellEndEdit(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridVie1.CellEndEdit 
    '... 
    '....editing codes here...to input and validate values... 
    '... 
    '... 
    '...before the End If of the procedure I put this values 
    DataGridVie1.Rows(e.RowIndex).Cells(e.ColumnIndex).Selected = True 
    DataGridVie1.CurrentCell = DataGridVie1.Rows(e.RowIndex).Cells(e.ColumnIndex) 
    'DataGridVie1.BeginEdit(False) '''DID NOT apply this because it cause to edit again. 
End If 

我不知道編輯後或ENTER KEY後焦點回到了編輯過的原始單元格中的真實代碼。

因爲每次我點擊ENTER鍵,就直接進入下一個單元格。

什麼是將焦點重新定位回原始單元格編輯的代碼。

我知道EditingControlShowing方法,但我不認爲我必須使用該方法才能得到我想要的。

回答

6

試試這個:定義3個變量。 一個記憶,如果編輯的操作已經做出,其他的最後2個編輯的單元格的儲存行和列索引:

Private flag_cell_edited As Boolean 
Private currentRow As Integer 
Private currentColumn As Integer 

當編輯操作時您存儲編輯的單元格的座標,和您的標誌設置

Private Sub DataGridView1_CellEndEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellEndEdit 
    flag_cell_edited = True 
    currentColumn = e.ColumnIndex 
    currentRow = e.RowIndex 
End Sub 

然後在SelectionChanged事件處理您在使用currentRowcurrentColumn變量撤消默認的單元格焦點更改設置DataGridViewCurrentCell財產最後編輯的單元格:內CellEndEdit事件處理真實:

Private Sub DataGridView1_SelectionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DataGridView1.SelectionChanged 
    If flag_cell_edited Then 
     DataGridView1.CurrentCell = DataGridView1(currentColumn, currentRow) 
     flag_cell_edited = False 
    End If 
End Sub 
+0

我不需要爲此方法考慮因爲你已經提供了。謝謝你,並stackoverflow。 – XXXXXXXXXXXXXX