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
事件类型
示例
下面的代码示例演示了如何处理此事件以清除行 DataGridViewRow.ErrorText 属性,以防它以前由 CellValidating 事件处理程序设置。 CellValidating当新单元格值满足验证条件时,事件处理程序可以清除错误文本,但当用户通过按 ESC 还原到旧单元格值时,CellValidating事件不会发生。 此示例是演练:验证 Windows 窗体 DataGridView 控件中的数据中提供的更大示例的一部分。
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
注解
有关如何处理事件的详细信息,请参阅 处理和引发事件。