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 class DataRowView : System.ComponentModel.ICustomTypeDescriptor, System.ComponentModel.IDataErrorInfo, System.ComponentModel.IEditableObject, System.ComponentModel.INotifyPropertyChanged
type DataRowView = class
    interface ICustomTypeDescriptor
    interface IEditableObject
    interface IDataErrorInfo
    interface INotifyPropertyChanged
Public Class DataRowView
Implements ICustomTypeDescriptor, IDataErrorInfo, IEditableObject, INotifyPropertyChanged
继承
DataRowView
实现

示例

下面的示例使用 RowVersion 属性来确定 DataRowView 中的行的状态。The 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.

DataRow 调用 BeginEdit 后,任何已编辑的值都将成为 Proposed 值。After invoking BeginEdit on a DataRow, any edited value becomes the Proposed value. 在调用 CancelEditEndEdit 之前,该行具有 OriginalProposed 版本。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()

返回一个表示当前对象的 string。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.

另请参阅