您需要處理EditingControlShowing事件。
Private Sub HandleDgvEditingControlShowing(sender As Object, e As DataGridViewEditingControlShowingEventArgs) Handles Dgv.EditingControlShowing
If (TypeOf e.Control Is DataGridViewTextBoxEditingControl) Then
With DirectCast(e.Control, DataGridViewTextBoxEditingControl)
.Text = "1/1/2001"
End With
End If
End Sub
請注意,此事件將針對所有可編輯單元格觸發。如果您只想操作給定列的編輯控件,則需要添加其他條件。事情是這樣的:
If (Me.Dgv.CurrentCell.OwningColumn is Me.DgvFooColumn) Then
如果您需要從CellValidating事件還原修改然後存儲原始值顯示在編輯控件時。
Private Sub HandleDgvEditingControlShowing(sender As Object, e As DataGridViewEditingControlShowingEventArgs) Handles Dgv.EditingControlShowing
'Cache the edit control text
Me.cachedEditText = e.Control.Text
End Sub
Private Sub HandleDgvCellValidating(sender As Object, e As DataGridViewCellValidatingEventArgs) Handles Dgv.CellValidating
'Ensure that the edit control exists
If (Not Me.Dgv.EditingControl Is Nothing) Then
'Validate the edit
If (Not valid) Then
Me.Dgv.EditingControl.Text = Me.cachedEditText
End If
End If
End Sub
Private cachedEditText As String
爲什麼不爲底層業務對象設置值呢? – Neolisk 2014-12-07 13:10:21