Share via


DataGridView.SelectedRows Properti

Definisi

Mendapatkan kumpulan baris yang dipilih oleh pengguna.

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

Nilai Properti

DataGridViewSelectedRowCollection yang berisi baris yang dipilih oleh pengguna.

Atribut

Contoh

Contoh kode berikut menunjukkan cara menggunakan SelectedRows properti dalam peristiwa tersebut UserDeletingRow . Dalam contoh ini, jika baris pertama terkandung dalam SelectedRows koleksi, maka penghapusan dibatalkan. Contoh ini adalah bagian dari contoh yang lebih besar yang tersedia dalam peristiwa tersebut 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

Keterangan

SelectionMode Properti harus diatur ke FullRowSelect atau RowHeaderSelect agar SelectedRows properti diisi dengan baris yang dipilih.

Properti ini berisi rekam jepret baca-saja dari pilihan pada saat dirujuk. Jika Anda menyimpan salinan koleksi ini, mungkin berbeda dari status aktual dan berikutnya DataGridView di mana pengguna mungkin telah mengubah pilihan. Oleh karena itu, Anda tidak boleh beroperasi pada salinan koleksi.

Untuk memilih baris secara terprogram, atur propertinya Selected ke true.

Berlaku untuk

Lihat juga