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.

適用於

另請參閱