DataRow 類別

定義

表示 DataTable 中的資料列。

public ref class DataRow
public class DataRow
[System.Serializable]
public class DataRow
type DataRow = class
[<System.Serializable>]
type DataRow = class
Public Class DataRow
繼承
DataRow
屬性

範例

下列範例會呼叫 NewRow 物件的 方法DataTable,以建立新的 DataRow

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;
}
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 New DataTable("Names") 

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

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

    Dim lNameColumn As 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

備註

DataRowDataColumn 物件是的主要元件DataTableDataRow使用物件及其屬性和方法來擷取和評估;以及插入、刪除和更新 中的DataTable值。 DataRowCollection表示 中DataTable的實際DataRow物件,而且 DataColumnCollection 包含DataColumn描述 架構的物件DataTable。 使用多載 Item[] 屬性可傳回或設定的值 DataColumn

HasVersion使用 和 IsNull 屬性來判斷特定數據列值的狀態,並使用 RowState 屬性來判斷相對於其父DataTable代 的數據列狀態。

若要建立新的 DataRow,請使用 NewRow 物件的方法 DataTable 。 建立新的 DataRow之後,請使用 Add 方法將新的 DataRow 新增至 DataRowCollection。 最後,呼叫 AcceptChanges 物件的方法 DataTable ,以確認新增。 如需將數據新增至 DataTable的詳細資訊,請參閱 將數據新增至 DataTable

您可以藉由呼叫 RemoveDataRowCollection方法,或呼叫 Delete 物件的 方法DataRow,從 DataRowCollection 中刪除 DataRow 。 方法 Remove 會從集合中移除數據列。 相反地, Delete 請標示 DataRow 要移除的 。 當您呼叫 AcceptChanges 方法時,就會發生實際的移除。 藉由呼叫 Delete,您可以透過程序設計方式檢查哪些數據列標示為移除,然後再實際刪除它們。 如需詳細資訊,請參閱 DataRow 刪除

建構函式

DataRow(DataRowBuilder)

使用指定的產生器,初始化 DataRow 類別的新實例。 僅供內部使用。

屬性

HasErrors

取得值,指出資料列中是否有錯誤。

Item[DataColumn, DataRowVersion]

取得儲存於指定的 DataColumn 中指定版本的資料。

Item[DataColumn]

取得或設定儲存於指定的 DataColumn 中的資料。

Item[Int32, DataRowVersion]

取得儲存於資料行中的資料,它是由要擷取的資料索引和版本所指定。

Item[Int32]

取得或設定儲存於索引指定的資料行中的資料。

Item[String, DataRowVersion]

取得儲存於具名資料行中指定版本的資料。

Item[String]

取得或設定儲存於名稱所指定之資料行的資料。

ItemArray

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

RowError

取得或設定資料列的自訂錯誤描述。

RowState

取得關於資料列與 DataRowCollection 之關聯性 (Relationship) 的資料列目前狀態。

Table

取得其中的這個資料列具有結構描述的 DataTable

方法

AcceptChanges()

認可自前一次呼叫 AcceptChanges() 以來,對這個資料列所做的所有變更。

BeginEdit()

DataRow 物件上開始編輯作業。

CancelEdit()

取消資料列上目前的編輯。

ClearErrors()

清除資料列的錯誤。 這包括 RowError 和以 SetColumnError(Int32, String) 設定的錯誤。

Delete()

刪除 DataRow

EndEdit()

結束資料列上發生的編輯。

Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
GetChildRows(DataRelation)

使用指定的 DataRow,取得這個 DataRelation 的子資料列。

GetChildRows(DataRelation, DataRowVersion)

使用指定的 DataRowDataRelation,取得 DataRowVersion 的子資料列。

GetChildRows(String)

使用 DataRow 的指定 RelationName,取得 DataRelation 的子資料列。

GetChildRows(String, DataRowVersion)

使用 DataRow 的指定 RelationNameDataRelation,取得 DataRowVersion 的子資料列。

GetColumnError(DataColumn)

取得指定 DataColumn 的錯誤描述。

GetColumnError(Int32)

取得索引指定的資料行的錯誤描述。

GetColumnError(String)

取得名稱指定的資料行的錯誤描述。

GetColumnsInError()

取得具有錯誤的資料行陣列。

GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetParentRow(DataRelation)

使用指定的 DataRow,取得 DataRelation 的父資料列。

GetParentRow(DataRelation, DataRowVersion)

使用指定的 DataRowDataRelation,取得 DataRowVersion 的父資料列。

GetParentRow(String)

使用 DataRow 的指定 RelationName,取得 DataRelation 的父資料列。

GetParentRow(String, DataRowVersion)

使用 DataRow 的指定 RelationNameDataRelation,取得 DataRowVersion 的父資料列。

GetParentRows(DataRelation)

使用指定的 DataRow,取得 DataRelation 的父資料列。

GetParentRows(DataRelation, DataRowVersion)

使用指定的 DataRowDataRelation,取得 DataRowVersion 的父資料列。

GetParentRows(String)

使用 DataRow 的指定 RelationName,取得 DataRelation 的父資料列。

GetParentRows(String, DataRowVersion)

使用 DataRow 的指定 RelationNameDataRelation,取得 DataRowVersion 的父資料列。

GetType()

取得目前執行個體的 Type

(繼承來源 Object)
HasVersion(DataRowVersion)

取得值,指出是否有指定的版本。

IsNull(DataColumn)

取得值,指出指定的 DataColumn 是否含有 Null 值。

IsNull(DataColumn, DataRowVersion)

取得值,指出指定的 DataColumnDataRowVersion 是否包含 Null 值。

IsNull(Int32)

取得值,指出指定之索引處的資料行是否包含 Null 值。

IsNull(String)

取得值,指出具名資料行是否包含 Null 值。

MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
RejectChanges()

拒絕自前一次呼叫 AcceptChanges() 以來,對資料列所做的所有變更。

SetAdded()

RowStateDataRow 變更為 Added

SetColumnError(DataColumn, String)

對指定為 DataColumn 的資料行設定錯誤描述。

SetColumnError(Int32, String)

設定索引指定的資料行的錯誤描述。

SetColumnError(String, String)

設定名稱指定的資料行的錯誤描述。

SetModified()

RowStateDataRow 變更為 Modified

SetNull(DataColumn)

將指定的 DataColumn 的值設定為 Null 值。

SetParentRow(DataRow)

使用指定的新父 DataRow,設定 DataRow 的父資料列。

SetParentRow(DataRow, DataRelation)

使用指定的新父 DataRowDataRow,設定 DataRelation 的父資料列。

ToString()

傳回代表目前物件的字串。

(繼承來源 Object)

擴充方法

Field<T>(DataRow, DataColumn)

提供強型別的權限以存取指定之資料列中每一個資料行的值。 Field<T>(DataRow, DataColumn) 方法也支援可為 null 的型別。

Field<T>(DataRow, DataColumn, DataRowVersion)

提供強型別的權限以存取指定之資料列中每一個資料行的值。 Field<T>(DataRow, DataColumn, DataRowVersion) 方法也支援可為 null 的型別。

Field<T>(DataRow, Int32)

提供強型別的權限以存取指定之資料列中每一個資料行的值。 Field<T>(DataRow, Int32) 方法也支援可為 null 的型別。

Field<T>(DataRow, Int32, DataRowVersion)

提供強型別的權限以存取指定之資料列中每一個資料行的值。 Field<T>(DataRow, Int32, DataRowVersion) 方法也支援可為 null 的型別。

Field<T>(DataRow, String)

提供強型別的權限以存取指定之資料列中每一個資料行的值。 Field<T>(DataRow, String) 方法也支援可為 null 的型別。

Field<T>(DataRow, String, DataRowVersion)

提供強型別的權限以存取指定之資料列中每一個資料行的值。 Field<T>(DataRow, String, DataRowVersion) 方法也支援可為 null 的型別。

SetField<T>(DataRow, DataColumn, T)

DataRow 中,為指定的資料行設定新的值。 SetField<T>(DataRow, DataColumn, T) 方法也支援可為 null 的型別。

SetField<T>(DataRow, Int32, T)

DataRow (會針對它呼叫此方法) 中,為指定的資料行設定新的值。 SetField<T>(DataRow, Int32, T) 方法也支援可為 null 的型別。

SetField<T>(DataRow, String, T)

DataRow 中,為指定的資料行設定新的值。 SetField<T>(DataRow, String, T) 方法也支援可為 null 的型別。

適用於

執行緒安全性

此類型適用於多線程讀取作業。 您必須同步處理任何寫入作業。

另請參閱