DataGridView.RowLeave Event

Definition

Occurs when a row loses input focus and is no longer the current row.

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

Event Type

Examples

The following code example illustrates how to handle this event to change the BackColor property of the cells in the current row. In this example, the background color is set in the RowEnter event, then reset to Empty on the RowLeave event. To run this example, paste the code into a form that contains a DataGridView named dataGridView1 and ensure that all events are associated with their event handlers.

C#
private void dataGridView1_RowEnter(object sender, 
    DataGridViewCellEventArgs e)
{
    for (int i = 0; i < dataGridView1.Rows[e.RowIndex].Cells.Count; i++)
    {
        dataGridView1[i, e.RowIndex].Style.BackColor = Color.Yellow;
    }
}

private void dataGridView1_RowLeave(object sender, 
    DataGridViewCellEventArgs e)
{
    for (int i = 0; i < dataGridView1.Rows[e.RowIndex].Cells.Count; i++)
    {
        dataGridView1[i, e.RowIndex].Style.BackColor = Color.Empty;
    }
}

Remarks

For more information about how to handle events, see Handling and Raising Events.

Applies to

Product Versions
.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

See also