DataRowView 類別

定義

表示 DataRow 的自訂檢視。Represents a customized view of a DataRow.

public ref class DataRowView : System::ComponentModel::ICustomTypeDescriptor, System::ComponentModel::IDataErrorInfo, System::ComponentModel::IEditableObject, System::ComponentModel::INotifyPropertyChanged
public ref class DataRowView : System::ComponentModel::ICustomTypeDescriptor, System::ComponentModel::IDataErrorInfo, System::ComponentModel::IEditableObject
public class DataRowView : System.ComponentModel.ICustomTypeDescriptor, System.ComponentModel.IDataErrorInfo, System.ComponentModel.IEditableObject, System.ComponentModel.INotifyPropertyChanged
public class DataRowView : System.ComponentModel.ICustomTypeDescriptor, System.ComponentModel.IDataErrorInfo, System.ComponentModel.IEditableObject
type DataRowView = class
    interface ICustomTypeDescriptor
    interface IDataErrorInfo
    interface IEditableObject
    interface INotifyPropertyChanged
type DataRowView = class
    interface ICustomTypeDescriptor
    interface IEditableObject
    interface IDataErrorInfo
type DataRowView = class
    interface ICustomTypeDescriptor
    interface IEditableObject
    interface IDataErrorInfo
    interface INotifyPropertyChanged
Public Class DataRowView
Implements ICustomTypeDescriptor, IDataErrorInfo, IEditableObject, INotifyPropertyChanged
Public Class DataRowView
Implements ICustomTypeDescriptor, IDataErrorInfo, IEditableObject
繼承
DataRowView
實作

範例

下列範例會使用 RowVersion 屬性來決定中的資料列狀態 DataRowViewThe following example uses the RowVersion property to determine the state of a row in the DataRowView. (請參閱 RowFilter ,以取得使用的另一個範例 DataRowView ) (See RowFilter for another example using DataRowView.)

private static void DemonstrateRowVersion()
{
    // Create a DataTable with one column.
    DataTable table = new DataTable("Table");
    DataColumn column = new DataColumn("Column");
    table.Columns.Add(column);

    // Add ten rows.
    DataRow row;
    for (int i = 0; i < 10; i++)
    {
        row = table.NewRow();
        row["Column"] = "item " + i;
        table.Rows.Add(row);
    }

    table.AcceptChanges();
    // Create a DataView with the table.
    DataView view = new DataView(table);

    // Change one row's value:
    table.Rows[1]["Column"] = "Hello";

    // Add one row:
    row = table.NewRow();
    row["Column"] = "World";
    table.Rows.Add(row);

    // Set the RowStateFilter to display only added
    // and modified rows.
    view.RowStateFilter = DataViewRowState.Added |
        DataViewRowState.ModifiedCurrent;

    // Print those rows. Output includes "Hello" and "World".
    PrintView(view, "ModifiedCurrent and Added");

    // Set filter to display only originals of modified rows.
    view.RowStateFilter = DataViewRowState.ModifiedOriginal;
    PrintView(view, "ModifiedOriginal");

    // Delete three rows.
    table.Rows[1].Delete();
    table.Rows[2].Delete();
    table.Rows[3].Delete();

    // Set the RowStateFilter to display only deleted rows.
    view.RowStateFilter = DataViewRowState.Deleted;
    PrintView(view, "Deleted");

    // Set filter to display only current rows.
    view.RowStateFilter = DataViewRowState.CurrentRows;
    PrintView(view, "Current");

    // Set filter to display only unchanged rows.
    view.RowStateFilter = DataViewRowState.Unchanged;
    PrintView(view, "Unchanged");

    // Set filter to display only original rows.
    // Current values of unmodified rows are also returned.
    view.RowStateFilter = DataViewRowState.OriginalRows;
    PrintView(view, "OriginalRows");
}

private static void PrintView(DataView view, string label)
{
    Console.WriteLine("\n" + label);
    for (int i = 0; i < view.Count; i++)
    {
        Console.WriteLine(view[i]["Column"]);
        Console.WriteLine("DataViewRow.RowVersion: {0}",
            view[i].RowVersion);
    }
}
Private Sub DemonstrateRowVersion()
    Dim i As Integer
    ' Create a DataTable with one column.
    Dim table As New DataTable("Table")
    Dim column As New DataColumn("Column")
    table.Columns.Add(column)

    ' Add ten rows.
    Dim row As DataRow
    For i = 0 To 9
        row = table.NewRow()
        row("Column") = "item " + i.ToString()
        table.Rows.Add(row)
    Next i
    table.AcceptChanges()

    ' Create a DataView with the table.
    Dim view As New DataView(table)

    ' Change one row's value:
    table.Rows(1)("Column") = "Hello"

    ' Add one row:
    row = table.NewRow()
    row("Column") = "World"
    table.Rows.Add(row)

    ' Set the RowStateFilter to display only added and modified rows.
    view.RowStateFilter = _
       DataViewRowState.Added Or DataViewRowState.ModifiedCurrent

    ' Print those rows. Output includes "Hello" and "World".
    PrintView(view, "ModifiedCurrent and Added")

    ' Set filter to display only originals of modified rows.
    view.RowStateFilter = DataViewRowState.ModifiedOriginal
    PrintView(view, "ModifiedOriginal")

    ' Delete three rows.
    table.Rows(1).Delete()
    table.Rows(2).Delete()
    table.Rows(3).Delete()

    ' Set the RowStateFilter to display only deleted rows.
    view.RowStateFilter = DataViewRowState.Deleted
    PrintView(view, "Deleted")

    ' Set filter to display only current rows.
    view.RowStateFilter = DataViewRowState.CurrentRows
    PrintView(view, "Current")

    ' Set filter to display only unchanged rows.
    view.RowStateFilter = DataViewRowState.Unchanged
    PrintView(view, "Unchanged")

    ' Set filter to display only original rows.
    ' Current values of unmodified rows are also returned.
    view.RowStateFilter = DataViewRowState.OriginalRows
    PrintView(view, "OriginalRows")
End Sub

Private Sub PrintView(ByVal view As DataView, ByVal label As String)
    Console.WriteLine(ControlChars.Cr + label)
    Dim i As Integer
    For i = 0 To view.Count - 1
        Console.WriteLine(view(i)("Column"))
        Console.WriteLine("DataRowView.RowVersion: {0}", _
            view(i).RowVersion)
    Next i
End Sub

備註

每次顯示資料時(例如在 DataGrid 控制項中),只能顯示每個資料列的一個版本。Whenever data is displayed, such as in a DataGrid control, only one version of each row can be displayed. 顯示的資料列是 DataRowViewThe displayed row is a DataRowView.

DataRowView可以有四種不同的版本狀態之一: DefaultOriginalCurrentProposedA DataRowView can have one of four different version states: Default, Original, Current, and Proposed.

BeginEdit在上叫用之後 DataRow ,任何已編輯的值都會變成 Proposed 值。After invoking BeginEdit on a DataRow, any edited value becomes the Proposed value. 在叫用 CancelEdit 或之後,資料列會 EndEditOriginalProposed 版本。Until either CancelEdit or EndEdit is invoked, the row has an Original and a Proposed version. 如果叫用 CancelEdit ,則會捨棄建議的版本,並將值還原為 OriginalIf CancelEdit is invoked, the proposed version is discarded, and the value reverts to Original. 如果叫 EndEdit 用,則 DataRowView 不再有版本, Proposed 而是建議的值成為目前的值。If EndEdit is invoked, the DataRowView no longer has a Proposed version; instead, the proposed value becomes the current value. 預設值僅適用于已定義預設值之資料行的資料列。Default values are available only on rows that have columns with default values defined.

屬性

DataView

取得這個資料列所屬的 DataViewGets the DataView to which this row belongs.

IsEdit

指示資料列是否處於編輯模式。Indicates whether the row is in edit mode.

IsNew

指示 DataRowView 是否為新的。Indicates whether a DataRowView is new.

Item[Int32]

取得或設定指定資料行中的值。Gets or sets a value in a specified column.

Item[String]

取得或設定指定資料行中的值。Gets or sets a value in a specified column.

Row

取得正在檢視的 DataRowGets the DataRow being viewed.

RowVersion

取得 DataRow 的目前版本描述。Gets the current version description of the DataRow.

方法

BeginEdit()

開始編輯程序。Begins an edit procedure.

CancelEdit()

取消編輯程序。Cancels an edit procedure.

CreateChildView(DataRelation)

使用指定的子 DataView,傳回子系 DataTableDataRelationReturns a DataView for the child DataTable with the specified child DataRelation.

CreateChildView(DataRelation, Boolean)

使用指定的 DataRelation 和父系,傳回子系 DataTableDataViewReturns a DataView for the child DataTable with the specified DataRelation and parent.

CreateChildView(String)

使用指定的子 DataView 名稱,傳回子系 DataTableDataRelationReturns a DataView for the child DataTable with the specified child DataRelation name.

CreateChildView(String, Boolean)

使用指定的 DataView 名稱和父代,傳回子系 DataTableDataRelationReturns a DataView for the child DataTable with the specified DataRelation name and parent.

Delete()

刪除資料列。Deletes a row.

EndEdit()

認可對基礎 DataRow 的變更,並結束以 BeginEdit() 開始的編輯階段作業。Commits changes to the underlying DataRow and ends the editing session that was begun with BeginEdit(). 使用CancelEdit()可放棄對DataRow所做的變更。Use CancelEdit() to discard the changes made to the DataRow.

Equals(Object)

取得值,指出目前的 DataRowView 是否與指定的物件相同。Gets a value indicating whether the current DataRowView is identical to the specified object.

GetHashCode()

傳回 DataRow 物件的雜湊程式碼。Returns the hash code of the DataRow object.

GetType()

取得目前執行個體的 TypeGets the Type of the current instance.

(繼承來源 Object)
MemberwiseClone()

建立目前 Object 的淺層複製。Creates a shallow copy of the current Object.

(繼承來源 Object)
ToString()

傳回代表目前物件的字串。Returns a string that represents the current object.

(繼承來源 Object)

事件

PropertyChanged

DataRowView 屬性變更時所引發的事件。Event that is raised when a DataRowView property is changed.

明確介面實作

ICustomTypeDescriptor.GetAttributes()

傳回元件之這個執行個體的自訂屬性集合。Returns a collection of custom attributes for this instance of a component.

ICustomTypeDescriptor.GetClassName()

傳回這個元件執行個體的類別名稱。Returns the class name of this instance of a component.

ICustomTypeDescriptor.GetComponentName()

傳回這個元件執行個體的名稱。Returns the name of this instance of a component.

ICustomTypeDescriptor.GetConverter()

傳回這個元件執行個體的類型轉換子。Returns a type converter for this instance of a component.

ICustomTypeDescriptor.GetDefaultEvent()

傳回這個元件執行個體的預設事件。Returns the default event for this instance of a component.

ICustomTypeDescriptor.GetDefaultProperty()

傳回這個元件執行個體的屬性。Returns the default property for this instance of a component.

ICustomTypeDescriptor.GetEditor(Type)

傳回元件的這個執行個體之指定類型編輯器。Returns an editor of the specified type for this instance of a component.

ICustomTypeDescriptor.GetEvents()

傳回這個元件執行個體的事件。Returns the events for this instance of a component.

ICustomTypeDescriptor.GetEvents(Attribute[])

傳回這個含指定屬性之元件執行個體的事件。Returns the events for this instance of a component with specified attributes.

ICustomTypeDescriptor.GetProperties()

傳回元件之這個執行個體的屬性。Returns the properties for this instance of a component.

ICustomTypeDescriptor.GetProperties(Attribute[])

傳回這個含指定屬性之元件執行個體的屬性。Returns the properties for this instance of a component with specified attributes.

ICustomTypeDescriptor.GetPropertyOwner(PropertyDescriptor)

傳回物件,包含指定的屬性描述項所描述的屬性。Returns an object that contains the property described by the specified property descriptor.

IDataErrorInfo.Error

取得說明物件驗證錯誤的訊息。Gets a message that describes any validation errors for the object.

IDataErrorInfo.Item[String]

使用指定的名稱取得屬性的錯誤訊息。Gets the error message for the property with the given name.

適用於

執行緒安全性

此類型對於多執行緒讀取作業而言是安全的。This type is safe for multithreaded read operations. 您必須同步處理任何寫入作業。You must synchronize any write operations.

另請參閱