DataGridView.CellEndEdit 事件

定义

当前选定单元格的编辑模式停止时发生。Occurs when edit mode stops for the currently selected cell.

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 

示例

下面的代码示例演示如何处理此事件,以便在之前已由 @no__t 1 事件处理程序设置时清除行 DataGridViewRow.ErrorText 属性。The following code example illustrates how to handle this event to clear the row DataGridViewRow.ErrorText property in case it was previously set by a CellValidating event handler. 当新单元格的值满足验证条件,但当用户通过按 ESC 恢复到旧单元值时,@no__t 0 事件处理程序可以清除错误文本,而不会发生 @no__t 1 事件。The CellValidating event handler can clear the error text when the new cell value meets the validation criteria, but when the user reverts to the old cell value by pressing ESC, the CellValidating event does not occur. 此示例是 [Walkthrough 中提供的一个更大示例的一部分:正在验证 Windows 窗体 DataGridView 控件 @ no__t 中的数据。This example is part of a larger example available in Walkthrough: Validating Data in the Windows Forms DataGridView Control.

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

注解

有关如何处理事件的详细信息,请参阅处理和引发事件For more information about how to handle events, see Handling and Raising Events.

适用于

另请参阅