DataRow 类

表示 DataTable 中的一行数据。

**命名空间:**System.Data
**程序集:**System.Data(在 system.data.dll 中)

语法

声明
Public Class DataRow
用法
Dim instance As DataRow
public class DataRow
public ref class DataRow
public class DataRow
public class DataRow

备注

DataRowDataColumn 对象是 DataTable 的主要组件。使用 DataRow 对象及其属性和方法检索、评估、插入、删除和更新 DataTable 中的值。DataRowCollection 表示 DataTable 中的实际 DataRow 对象,DataColumnCollection 中包含用于描述 DataTable 的架构的 DataColumn 对象。使用重载的 Item 属性返回或设置 DataColumn 的值。

使用 HasVersionIsNull 属性确定特定行值的状态,使用 RowState 属性确定行相对于它的父级 DataTable 的状态。

若要创建新的 DataRow,请使用 DataTable 对象的 NewRow 方法。创建新的 DataRow 之后,请使用 Add 方法将新的 DataRow 添加到 DataRowCollection 中。最后,调用 DataTable 对象的 AcceptChanges 方法以确认是否已添加。有关将数据添加到 DataTable 中的更多信息,请参见 将数据添加到表中

您可通过调用 DataRowCollectionRemove 方法或调用 DataRow 对象的 Delete 方法,从 DataRowCollection 中删除 DataRowRemove 方法将行从集合中移除。与此相反,Delete 标记要移除的 DataRow。在调用 AcceptChanges 方法时发生实际移除。通过调用 Delete,您可在实际删除行之前以编程方式检查哪些行被标记为移除。有关更多信息,请参见 从表中删除行

示例

下面的示例通过调用 DataTable 对象的 NewRow 方法创建新的 DataRow

Private Sub CreateNewDataRow()
    ' Use the MakeTable function below to create a new table.
    Dim table As DataTable
    table = MakeNamesTable()

    ' Once a table has been created, use the 
    ' NewRow to create a DataRow.
    Dim row As DataRow 
    row = table.NewRow()

    ' Then add the new row to the collection.
    row("fName") = "John"
    row("lName") = "Smith"
    table.Rows.Add(row)
    
    Dim column As DataColumn
    For Each column in table.Columns
       Console.WriteLine(column.ColumnName)
    Next
    DataGrid1.DataSource=table
 End Sub
 
 Private Function MakeNamesTable() As DataTable
    ' Create a new DataTable titled 'Names.'
    Dim namesTable As DataTable = new DataTable("Names") 

    ' Add three column objects to the table.
    Dim idColumn As DataColumn = new  DataColumn()
    idColumn.DataType = System.Type.GetType("System.Int32")
    idColumn.ColumnName = "id"
    idColumn.AutoIncrement = True
    namesTable.Columns.Add(idColumn)

    Dim fNameColumn As DataColumn = New DataColumn()
    fNameColumn.DataType = System.Type.GetType("System.String")
    fNameColumn.ColumnName = "Fname"
    fNameColumn.DefaultValue = "Fname"
    namesTable.Columns.Add(fNameColumn)

    Dim lNameColumn As DataColumn = new DataColumn()
    lNameColumn.DataType = System.Type.GetType("System.String")
    lNameColumn.ColumnName = "LName"
    namesTable.Columns.Add(lNameColumn)

    ' Create an array for DataColumn objects.
    Dim keys(0) As DataColumn 
    keys(0) = idColumn
    namesTable.PrimaryKey = keys

    ' Return the new DataTable.
    MakeNamesTable = namesTable
 End Function
private void CreateNewDataRow()
{
    // Use the MakeTable function below to create a new table.
    DataTable table;
    table = MakeNamesTable();

    // Once a table has been created, use the 
    // NewRow to create a DataRow.
    DataRow row;
    row = table.NewRow();

    // Then add the new row to the collection.
    row["fName"] = "John";
    row["lName"] = "Smith";
    table.Rows.Add(row);

    foreach(DataColumn column in table.Columns)
        Console.WriteLine(column.ColumnName);
    dataGrid1.DataSource=table;
}
 
private DataTable MakeNamesTable()
{
    // Create a new DataTable titled 'Names.'
    DataTable namesTable = new DataTable("Names"); 

    // Add three column objects to the table.
    DataColumn idColumn = new  DataColumn();
    idColumn.DataType = System.Type.GetType("System.Int32");
    idColumn.ColumnName = "id";
    idColumn.AutoIncrement = true;
    namesTable.Columns.Add(idColumn);

    DataColumn fNameColumn = new DataColumn();
    fNameColumn.DataType = System.Type.GetType("System.String");
    fNameColumn.ColumnName = "Fname";
    fNameColumn.DefaultValue = "Fname";
    namesTable.Columns.Add(fNameColumn);

    DataColumn lNameColumn = new DataColumn();
    lNameColumn.DataType = System.Type.GetType("System.String");
    lNameColumn.ColumnName = "LName";
    namesTable.Columns.Add(lNameColumn);

    // Create an array for DataColumn objects.
    DataColumn [] keys = new DataColumn [1];
    keys[0] = idColumn;
    namesTable.PrimaryKey = keys;

    // Return the new DataTable.
    return namesTable;
}

继承层次结构

System.Object
  System.Data.DataRow

线程安全

该类型对于多线程读操作是安全的。您必须使任何写操作同步。

平台

Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

.NET Compact Framework

受以下版本支持:2.0、1.0

请参见

参考

DataRow 成员
System.Data 命名空间
AcceptChanges
Add
DataColumnCollection 类
DataColumn 类
DataRowView
DataTable
HasVersion
IsNull
Item
NewRow
DataRowCollection