DataTable.NewRow 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
创建与该表具有相同架构的新 DataRow。
public:
System::Data::DataRow ^ NewRow();
public System.Data.DataRow NewRow ();
member this.NewRow : unit -> System.Data.DataRow
Public Function NewRow () As DataRow
返回
示例
以下示例创建 ,添加两DataColumn个DataTable确定表架构的 对象,并使用 方法创建几个新DataRow对象NewRow。 DataRow然后使用 方法将这些对象添加到 。DataRowCollectionAdd
private void MakeDataTableAndDisplay()
{
// Create new DataTable and DataSource objects.
DataTable table = new DataTable();
// Declare DataColumn and DataRow variables.
DataColumn column;
DataRow row;
DataView view;
// Create new DataColumn, set DataType, ColumnName and add to DataTable.
column = new DataColumn();
column.DataType = System.Type.GetType("System.Int32");
column.ColumnName = "id";
table.Columns.Add(column);
// Create second column.
column = new DataColumn();
column.DataType = Type.GetType("System.String");
column.ColumnName = "item";
table.Columns.Add(column);
// Create new DataRow objects and add to DataTable.
for(int i = 0; i < 10; i++)
{
row = table.NewRow();
row["id"] = i;
row["item"] = "item " + i.ToString();
table.Rows.Add(row);
}
// Create a DataView using the DataTable.
view = new DataView(table);
// Set a DataGrid control's DataSource to the DataView.
dataGrid1.DataSource = view;
}
Private Sub MakeDataTableAndDisplay()
' Create new DataTable and DataSource objects.
Dim table As New DataTable()
' Declare DataColumn and DataRow variables.
Dim column As DataColumn
Dim row As DataRow
Dim view As DataView
' Create new DataColumn, set DataType, ColumnName and add to DataTable.
column = New DataColumn()
column.DataType = System.Type.GetType("System.Int32")
column.ColumnName = "id"
table.Columns.Add(column)
' Create second column.
column = New DataColumn()
column.DataType = Type.GetType("System.String")
column.ColumnName = "item"
table.Columns.Add(column)
' Create new DataRow objects and add to DataTable.
Dim i As Integer
For i = 0 to 9
row = table.NewRow()
row("id") = i
row("item") = "item " & i
table.Rows.Add(row)
Next
' Create a DataView using the DataTable.
view = New DataView(table)
' Set a DataGrid control's DataSource to the DataView.
DataGrid1.DataSource = view
End Sub
注解
必须使用 NewRow 方法创建架构与 相同的DataTable新 DataRow 对象。 创建 DataRow后,可以通过 对象的 Rows 属性将其添加到 DataRowCollection。DataTable 使用 NewRow 创建新行时,必须在调用 Clear之前在数据表中添加或删除行。