共用方式為


DataRow.ItemArray 屬性

定義

透過陣列取得或設定這個資料列的所有值。

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 的陣列。

例外狀況

陣列大於資料表中的資料行數目。

陣列中的值與其在個別的 DataType 中之 DataColumn 不相符。

編輯中斷條件約束。

編輯嘗試變更唯讀資料行的值。

某項編輯作業嘗試將 null 值置於資料行中,而該資料行中 AllowDBNull 物件的 DataColumnfalse

資料列已經刪除。

範例

下列範例示範如何使用 屬性取得和設定值 ItemArray

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

備註

您可以使用這個屬性,透過數位來設定或取得此資料列的值。 如果您使用這個屬性來設定值,數位的大小和順序必須與資料行集合相同。 nullItemArray傳入 表示未指定任何值。

用戶可以在事件或RowChanging事件中ColumnChanging產生例外狀況。

適用於

另請參閱