DataRow.ItemArray 属性

定义

通过数组获取或设置此行的所有值。Gets or sets all the values for this row through an array.

public:
 property cli::array <System::Object ^> ^ ItemArray { cli::array <System::Object ^> ^ get(); void set(cli::array <System::Object ^> ^ value); };
public object?[] ItemArray { get; set; }
public object[] ItemArray { get; set; }
member this.ItemArray : obj[] with get, set
Public Property ItemArray As Object()

属性值

Object[]

Object 类型的数组。An array of type Object.

例外

该数组大于表中的列数。The array is larger than the number of columns in the table.

数组中的值与其相应的 DataColumn 中的 DataType 不匹配。A value in the array does not match its DataType in its respective DataColumn.

编辑操作破坏了约束。An edit broke a constraint.

编辑操作尝试更改只读列的值。An edit tried to change the value of a read-only column.

编辑操作尝试将 null 值放在 DataColumn 对象的 AllowDBNullfalse 的列中。An edit tried to put a null value in a column where AllowDBNull of the DataColumn object is false.

已删除该行。The row has been deleted.

示例

下面的示例演示如何使用属性获取和设置值 ItemArrayThe following examples show how to get and set values using the ItemArray property.

private void CreateRowsWithItemArray()
{
    // Make a DataTable using the function below.
    DataTable dt = MakeTableWithAutoIncrement();
    DataRow relation;
    // Declare the array variable.
    object [] rowArray = new object[2];
    // Create 10 new rows and add to DataRowCollection.
    for(int i = 0; i <10; i++)
    {
        rowArray[0]=null;
        rowArray[1]= "item " + i;
        relation = dt.NewRow();
        relation.ItemArray = rowArray;
        dt.Rows.Add(relation);
    }
    PrintTable(dt);
}

private DataTable MakeTableWithAutoIncrement()
{
    // Make a table with one AutoIncrement column.
    DataTable table = new DataTable("table");
    DataColumn idColumn = new DataColumn("id",
        Type.GetType("System.Int32"));
    idColumn.AutoIncrement = true;
    idColumn.AutoIncrementSeed = 10;
    table.Columns.Add(idColumn);

    DataColumn firstNameColumn = new DataColumn("Item",
        Type.GetType("System.String"));
    table.Columns.Add(firstNameColumn);
    return table;
}

private void PrintTable(DataTable table)
{
    foreach(DataRow row in table.Rows)
    {
        foreach(DataColumn column in table.Columns)
        {
            Console.WriteLine(row[column]);
        }
    }
}
Private Sub CreateRowsWithItemArray()
    ' Make a DataTable using the function below.
    Dim dt As DataTable = MakeTableWithAutoIncrement()
    Dim relation As DataRow

    ' Declare the array variable.
    Dim rowArray(1) As Object

    ' Create 10 new rows and add to DataRowCollection.
    Dim i As Integer
    For i = 0 to 9
       rowArray(0) = DBNull.Value
       rowArray(1)= "item " & i.ToString()
       relation = dt.NewRow()
       relation.ItemArray = rowArray
       dt.Rows.Add(relation)
    Next
    PrintTable(dt)
End Sub
 
Private Function MakeTableWithAutoIncrement() As DataTable
    ' Make a table with one AutoIncrement column.
    Dim table As New DataTable("table")
    Dim idColumn As New DataColumn("id", _
        Type.GetType("System.Int32"))
    idColumn.AutoIncrement = True
    idColumn.AutoIncrementSeed = 10
    table.Columns.Add (idColumn)
    
    Dim firstNameColumn As New DataColumn( _
        "Item", Type.GetType("System.String"))
    table.Columns.Add(firstNameColumn)
    MakeTableWithAutoIncrement = table
End Function
 
Private Sub PrintTable(table As DataTable)
    Dim row As DataRow
    Dim column As DataColumn
    For Each row in table.Rows
       For Each column in table.Columns
          Console.WriteLine(row(column))
       Next
    Next
End Sub

注解

您可以使用此属性通过数组设置或获取此行的值。You can use this property to set or get values for this row through an array. 如果使用此属性设置值,则数组的大小和排序方式必须与列集合的大小相同。If you use this property to set values, the array must have the same size and ordering as the column collection. 传入 nullItemArray 指示未指定任何值。Passing null in the ItemArray indicates that no value was specified.

用户可以在 ColumnChanging 事件或事件中生成异常 RowChangingUsers can generate exceptions in the ColumnChanging event or the RowChanging event.

适用于

另请参阅