DataTable.Rows Properti

Definisi

Mendapatkan kumpulan baris milik tabel ini.

public:
 property System::Data::DataRowCollection ^ Rows { System::Data::DataRowCollection ^ get(); };
[System.ComponentModel.Browsable(false)]
public System.Data.DataRowCollection Rows { get; }
[System.ComponentModel.Browsable(false)]
[System.Data.DataSysDescription("DataTableRowsDescr")]
public System.Data.DataRowCollection Rows { get; }
[<System.ComponentModel.Browsable(false)>]
member this.Rows : System.Data.DataRowCollection
[<System.ComponentModel.Browsable(false)>]
[<System.Data.DataSysDescription("DataTableRowsDescr")>]
member this.Rows : System.Data.DataRowCollection
Public ReadOnly Property Rows As DataRowCollection

Nilai Properti

yang DataRowCollection berisi DataRow objek.

Atribut

Contoh

Berikut ini memperlihatkan dua contoh baris pengembalian dan pengaturan. Contoh pertama menggunakan Rows properti dan mencetak nilai setiap kolom untuk setiap baris. Contoh kedua menggunakan DataTable metode objek NewRow untuk membuat objek baru DataRow dengan skema DataTable. Setelah mengatur nilai baris, baris ditambahkan ke DataRowCollection melalui Add metode .

private void PrintRows(DataSet dataSet)
{
    // For each table in the DataSet, print the values of each row.
    foreach(DataTable thisTable in dataSet.Tables)
    {
        // For each row, print the values of each column.
        foreach(DataRow row in thisTable.Rows)
        {
            foreach(DataColumn column in thisTable.Columns)
            {
                Console.WriteLine(row[column]);
            }
        }
    }
}

private void AddARow(DataSet dataSet)
{
    DataTable table;
    table = dataSet.Tables["Suppliers"];
    // Use the NewRow method to create a DataRow with
    // the table's schema.
    DataRow newRow = table.NewRow();

    // Set values in the columns:
    newRow["CompanyID"] = "NewCompanyID";
    newRow["CompanyName"] = "NewCompanyName";

    // Add the row to the rows collection.
    table.Rows.Add(newRow);
}
Private Sub PrintRows(dataSet As DataSet)
     ' For each table in the DataSet, print the values of each row.
     Dim thisTable As DataTable
     For Each thisTable In  dataSet.Tables
         ' For each row, print the values of each column.
         Dim row As DataRow
         For Each row In  thisTable.Rows
             Dim column As DataColumn
             For Each column In  thisTable.Columns
                 Console.WriteLine(row(column))
             Next column
         Next row
     Next thisTable
End Sub
    
    
Private Sub AddARow(dataSet As DataSet)
    Dim table As DataTable = dataSet.Tables("Suppliers")
    ' Use the NewRow method to create a DataRow 
    'with the table's schema.
    Dim newRow As DataRow = table.NewRow()

    ' Set values in the columns:
    newRow("CompanyID") = "NewCompanyID"
    newRow("CompanyName") = "NewCompanyName"

    ' Add the row to the rows collection.
    table.Rows.Add(newRow)
End Sub

Keterangan

Untuk membuat baru DataRow, Anda harus menggunakan NewRow metode untuk mengembalikan objek baru. Objek seperti itu secara otomatis dikonfigurasi sesuai dengan skema yang DataTable ditentukan untuk melalui koleksi DataColumn objeknya. Setelah membuat baris baru dan mengatur nilai untuk setiap kolom dalam baris, tambahkan baris ke DataRowCollection menggunakan Add metode .

Masing-masing DataRow dalam koleksi mewakili baris data dalam tabel. Untuk menerapkan perubahan pada nilai kolom dalam baris, Anda harus memanggil AcceptChanges metode .

Berlaku untuk

Lihat juga