DataGridView.CellEndEdit 事件
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
目前所選取儲存格的編輯模式停止時發生。
public:
event System::Windows::Forms::DataGridViewCellEventHandler ^ CellEndEdit;
public event System.Windows.Forms.DataGridViewCellEventHandler CellEndEdit;
public event System.Windows.Forms.DataGridViewCellEventHandler? CellEndEdit;
member this.CellEndEdit : System.Windows.Forms.DataGridViewCellEventHandler
Public Custom Event CellEndEdit As DataGridViewCellEventHandler
事件類型
範例
下列程式碼範例說明如何處理此事件,以清除先前由 CellValidating 事件處理常式設定的資料列 DataGridViewRow.ErrorText 屬性。 CellValidating當新的儲存格值符合驗證準則時,事件處理常式可以清除錯誤文字,但當使用者按下 ESC 還原為舊儲存格值時, CellValidating 則不會發生此事件。 此範例是逐步解說:驗證 DataGridView 控制項中資料Windows Forms較大範例的一部分。
private void dataGridView1_CellValidating(object sender,
DataGridViewCellValidatingEventArgs e)
{
string headerText =
dataGridView1.Columns[e.ColumnIndex].HeaderText;
// Abort validation if cell is not in the CompanyName column.
if (!headerText.Equals("CompanyName")) return;
// Confirm that the cell is not empty.
if (string.IsNullOrEmpty(e.FormattedValue.ToString()))
{
dataGridView1.Rows[e.RowIndex].ErrorText =
"Company Name must not be empty";
e.Cancel = true;
}
}
void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
// Clear the row error in case the user presses ESC.
dataGridView1.Rows[e.RowIndex].ErrorText = String.Empty;
}
Private Sub dataGridView1_CellValidating(ByVal sender As Object, _
ByVal e As DataGridViewCellValidatingEventArgs) _
Handles dataGridView1.CellValidating
Dim headerText As String = _
dataGridView1.Columns(e.ColumnIndex).HeaderText
' Abort validation if cell is not in the CompanyName column.
If Not headerText.Equals("CompanyName") Then Return
' Confirm that the cell is not empty.
If (String.IsNullOrEmpty(e.FormattedValue.ToString())) Then
dataGridView1.Rows(e.RowIndex).ErrorText = _
"Company Name must not be empty"
e.Cancel = True
End If
End Sub
Private Sub dataGridView1_CellEndEdit(ByVal sender As Object, _
ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) _
Handles dataGridView1.CellEndEdit
' Clear the row error in case the user presses ESC.
dataGridView1.Rows(e.RowIndex).ErrorText = String.Empty
End Sub
備註
如需如何處理事件的詳細資訊,請參閱 處理和引發事件。