DataGridView.SelectedRows 속성

정의

사용자가 선택한 행의 컬렉션을 가져옵니다.

public:
 property System::Windows::Forms::DataGridViewSelectedRowCollection ^ SelectedRows { System::Windows::Forms::DataGridViewSelectedRowCollection ^ get(); };
[System.ComponentModel.Browsable(false)]
public System.Windows.Forms.DataGridViewSelectedRowCollection SelectedRows { get; }
[<System.ComponentModel.Browsable(false)>]
member this.SelectedRows : System.Windows.Forms.DataGridViewSelectedRowCollection
Public ReadOnly Property SelectedRows As DataGridViewSelectedRowCollection

속성 값

사용자가 선택한 행이 들어 있는 DataGridViewSelectedRowCollection입니다.

특성

예제

다음 코드 예제를 사용 하는 방법을 보여 줍니다 합니다 SelectedRows 속성에 이벤트 UserDeletingRow . 이 예제에서 첫 번째 행이 컬렉션에 SelectedRows 포함되어 있으면 삭제가 취소됩니다. 이 예제는에서 사용할 수 있는 보다 큰 예제의 일부는 SelectionChanged 이벤트입니다.

private void DataGridView1_UserDeletingRow(object sender,
    DataGridViewRowCancelEventArgs e)
{
    DataGridViewRow startingBalanceRow = DataGridView1.Rows[0];

    // Check if the Starting Balance row is included in the selected rows
    if (DataGridView1.SelectedRows.Contains(startingBalanceRow))
    {
        // Do not allow the user to delete the Starting Balance row.
        MessageBox.Show("Cannot delete Starting Balance row!");

        // Cancel the deletion if the Starting Balance row is included.
        e.Cancel = true;
    }
}
Private Sub UserDeletingRow(ByVal sender As Object, _
    ByVal e As DataGridViewRowCancelEventArgs) _
    Handles DataGridView1.UserDeletingRow

    Dim startingBalanceRow As DataGridViewRow = DataGridView1.Rows(0)

    ' Check if the starting balance row is included in the selected rows
    If DataGridView1.SelectedRows.Contains(startingBalanceRow) Then
        ' Do not allow the user to delete the Starting Balance row.
        MessageBox.Show("Cannot delete Starting Balance row!")

        ' Cancel the deletion if the Starting Balance row is included.
        e.Cancel = True
    End If
End Sub

설명

속성이 SelectionMode 선택한 행으로 FullRowSelect 채워지려면 속성을 또는 RowHeaderSelectSelectedRows 로 설정해야 합니다.

이 속성은 참조할 때 선택 영역의 읽기 전용 스냅샷 포함합니다. 이 컬렉션의 복사본을 보관하는 경우 사용자가 선택을 변경했을 수 있는 실제 후속 DataGridView 상태와 다를 수 있습니다. 따라서 컬렉션의 복사본에서 작동하지 않아야 합니다.

프로그래밍 방식으로 행을 선택하려면 해당 Selected 속성을 true로 설정합니다.

적용 대상

추가 정보