DataGridView.RowEnter 事件

定义

在某一行接收到输入焦点但变成当前行之前发生。Occurs when a row receives input focus but before it becomes the current row.

public:
 event System::Windows::Forms::DataGridViewCellEventHandler ^ RowEnter;
public event System.Windows.Forms.DataGridViewCellEventHandler RowEnter;
member this.RowEnter : System.Windows.Forms.DataGridViewCellEventHandler 
Public Custom Event RowEnter As DataGridViewCellEventHandler 

示例

下面的代码示例演示如何处理此事件以更改当前行中单元格的 BackColor 属性。The following code example illustrates how to handle this event to change the BackColor property of the cells in the current row. 在此示例中,背景色在 RowEnter 事件中设置,然后重置为 RowLeave 事件 EmptyIn this example, the background color is set in the RowEnter event, then reset to Empty on the RowLeave event. 若要运行此示例,请将代码粘贴到包含名为 dataGridView1DataGridView 的窗体中,并确保所有事件都与它们的事件处理程序相关联。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.

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;
    }
}
Private Sub dataGridView1_RowEnter(ByVal sender As Object, _
    ByVal e As DataGridViewCellEventArgs) _
    Handles dataGridView1.RowEnter

    Dim i As Integer
    For i = 0 To dataGridView1.Rows(e.RowIndex).Cells.Count - 1
        dataGridView1(i, e.RowIndex).Style _
            .BackColor = Color.Yellow
    Next i

End Sub

Private Sub dataGridView1_RowLeave(ByVal sender As Object, _
    ByVal e As DataGridViewCellEventArgs) _
    Handles dataGridView1.RowLeave

    Dim i As Integer
    For i = 0 To dataGridView1.Rows(e.RowIndex).Cells.Count - 1
        dataGridView1(i, e.RowIndex).Style _
            .BackColor = Color.Empty
    Next i

End Sub

注解

当最初加载 DataGridView 时,以及当用户选择的行不是当前行时,将发生此事件。This event occurs when the DataGridView is initially loaded, as well as when the user selects a row other than the current row.

此事件发生在更新 CurrentRow 属性之前。This event occurs before the CurrentRow property is updated. 若要检索新输入行的索引,请使用事件处理程序中的 DataGridViewCellEventArgs.RowIndex 属性。To retrieve the index of the newly-entered row, use the DataGridViewCellEventArgs.RowIndex property within the event handler.

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

适用于

另请参阅