.NET Framework 类库 |
获取属于该表的行的集合。
[Visual Basic] Public ReadOnly Property Rows As DataRowCollection [C#] public DataRowCollection Rows {get;} [C++] public: __property DataRowCollection* get_Rows(); [JScript] public function get Rows() : DataRowCollection;
属性值
包含 DataRow 对象的 DataRowCollection;否则为空值(如果不存在任何 DataRow 对象)。
备注
若要创建新的 DataRow,必须使用 NewRow 方法返回新对象。这样的对象将按照架构自动配置,该架构是通过 DataColumn 对象的集合为 DataTable 定义的。在创建新行并为行中每个列设置值之后,使用 Add 方法将该行添加到 DataRowCollection 中。
集合中的每个 DataRow 表示表中的一行数据。若要提交对行中某一列的值的更改,必须调用 AcceptChanges 方法。
示例
[Visual Basic, C#, C++] 下面显示两个返回和设置行的示例。第一个示例使用 Rows 属性,并为每一行打印每一列的值。第二个示例使用 DataTable 对象的 NewRow 方法创建具有 DataTable 的架构的新 DataRow 对象。在设置行值之后,通过 Add 方法将该行添加到 DataRowCollection 中。
[Visual Basic] Private Sub PrintRows(myDataSet As DataSet) ' For each table in the DataSet, print the values of each row. Dim thisTable As DataTable For Each thisTable In myDataSet.Tables ' For each row, print the values of each column. Dim myRow As DataRow For Each myRow In thisTable.Rows Dim myCol As DataColumn For Each myCol In thisTable.Columns Console.WriteLine(myRow(myCol)) Next myCol Next myRow Next thisTable End Sub Private Sub AddARow(ds As DataSet) Dim t As DataTable t = ds.Tables("Suppliers") ' Use the NewRow method to create a DataRow with the table's schema. Dim newRow As DataRow = t.NewRow() ' Set values in the columns: newRow("CompanyID") = "NewCompanyID" newRow("CompanyName") = "NewCompanyName" ' Add the row to the rows collection. t.Rows.Add(newRow) End Sub [C#] private void PrintRows(DataSet myDataSet){ // For each table in the DataSet, print the values of each row. foreach(DataTable thisTable in myDataSet.Tables){ // For each row, print the values of each column. foreach(DataRow myRow in thisTable.Rows){ foreach(DataColumn myCol in thisTable.Columns){ Console.WriteLine(myRow[myCol]); } } } } private void AddARow(DataSet ds){ DataTable t; t = ds.Tables["Suppliers"]; // Use the NewRow method to create a DataRow with the table's schema. DataRow newRow = t.NewRow(); // Set values in the columns: newRow["CompanyID"] = "NewCompanyID"; newRow["CompanyName"] = "NewCompanyName"; // Add the row to the rows collection. t.Rows.Add(newRow); } [C++] private: void PrintRows(DataSet* myDataSet){ // For each table in the DataSet, print the values of each row. System::Collections::IEnumerator* myEnum = myDataSet->Tables->GetEnumerator(); while (myEnum->MoveNext()) { DataTable* thisTable = __try_cast<DataTable*>(myEnum->Current); // For each row, print the values of each column. System::Collections::IEnumerator* myEnum1 = thisTable->Rows->GetEnumerator(); while (myEnum1->MoveNext()) { DataRow* myRow = __try_cast<DataRow*>(myEnum1->Current); System::Collections::IEnumerator* myEnum2 = thisTable->Columns->GetEnumerator(); while (myEnum2->MoveNext()) { DataColumn* myCol = __try_cast<DataColumn*>(myEnum2->Current); Console::WriteLine(myRow->Item[myCol]); } } } } void AddARow(DataSet* ds){ DataTable* t; t = ds->Tables->Item[S"Suppliers"]; // Use the NewRow method to create a DataRow with the table's schema. DataRow* newRow = t->NewRow(); // Set values in the columns: newRow->Item[S"CompanyID"] = S"NewCompanyID"; newRow->Item[S"CompanyName"] = S"NewCompanyName"; // Add the row to the rows collection. t->Rows->Add(newRow); }
[JScript] 没有可用于 JScript 的示例。若要查看 Visual Basic、C# 或 C++ 示例,请单击页左上角的“语言筛选器”按钮 。
要求
平台: Windows 98, Windows NT 4.0, Windows ME, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 系列, .NET Framework 精简版
请参见
DataTable 类 | DataTable 成员 | System.Data 命名空间 | AcceptChanges | DataRow | DataRowCollection | NewRow