DataGridView.CellLeave 事件

定义

单元格失去输入焦点并且不再是当前单元格时发生。

C#
public event System.Windows.Forms.DataGridViewCellEventHandler CellLeave;
C#
public event System.Windows.Forms.DataGridViewCellEventHandler? CellLeave;

事件类型

示例

下面的代码示例演示如何处理此事件以更改 SelectionBackColor 当前单元格的 属性。 在此示例中,选择背景色在 事件中CellEnter设置,然后在 事件上CellLeave重置为 Empty 。 若要运行此示例,请将代码粘贴到包含命名dataGridView1DataGridView窗体中,并确保所有事件都与其事件处理程序相关联。

C#
private void dataGridView1_CellEnter(object sender, 
    DataGridViewCellEventArgs e)
{
    dataGridView1[e.ColumnIndex, e.RowIndex].Style
        .SelectionBackColor = Color.Blue;
}

private void dataGridView1_CellLeave(object sender, 
    DataGridViewCellEventArgs e)
{
    dataGridView1[e.ColumnIndex, e.RowIndex].Style
        .SelectionBackColor = Color.Empty;
}

注解

有关如何处理事件的详细信息,请参阅 处理和引发事件

适用于

产品 版本
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10

另请参阅