共用方式為


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,包含使用者選取的資料列。

屬性

範例

下列程式碼範例示範如何在 事件中使用 SelectedRowsUserDeletingRow 屬性。 在此範例中,如果集合中包含 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 必須設定為 FullRowSelectRowHeaderSelectSelectedRows ,屬性才能填入選取的資料列。

此屬性包含參考時選取範圍的唯讀快照集。 如果您保留此集合的複本,它可能與使用者可能已變更選取範圍的實際後續 DataGridView 狀態不同。 因此,您不應該在集合的複本上運作。

若要以程式設計方式選取資料列,請將其 Selected 屬性設定為 true

適用於

另請參閱