DataView 类

定义

代表 DataTable 的可绑定数据的自定义视图,它用于排序、筛选、搜索、编辑和导航。 DataView 不存储数据,而改为表示对应的 DataTable 的连接视图。 更改 DataView 的数据会影响 DataTable。 更改 DataTable 的数据将影响与之关联的所有 DataView

public ref class DataView : System::ComponentModel::MarshalByValueComponent, System::Collections::IList, System::ComponentModel::IBindingListView, System::ComponentModel::ISupportInitialize, System::ComponentModel::ISupportInitializeNotification, System::ComponentModel::ITypedList
public ref class DataView : System::ComponentModel::MarshalByValueComponent, System::Collections::IList, System::ComponentModel::IBindingList, System::ComponentModel::ISupportInitialize, System::ComponentModel::ITypedList
public ref class DataView : System::ComponentModel::MarshalByValueComponent, System::Collections::IList, System::ComponentModel::IBindingListView, System::ComponentModel::ISupportInitializeNotification, System::ComponentModel::ITypedList
public ref class DataView : System::ComponentModel::MarshalByValueComponent, System::ComponentModel::IBindingListView, System::ComponentModel::ISupportInitializeNotification, System::ComponentModel::ITypedList
public class DataView : System.ComponentModel.MarshalByValueComponent, System.Collections.IList, System.ComponentModel.IBindingListView, System.ComponentModel.ISupportInitialize, System.ComponentModel.ISupportInitializeNotification, System.ComponentModel.ITypedList
public class DataView : System.ComponentModel.MarshalByValueComponent, System.Collections.IList, System.ComponentModel.IBindingList, System.ComponentModel.ISupportInitialize, System.ComponentModel.ITypedList
public class DataView : System.ComponentModel.MarshalByValueComponent, System.Collections.IList, System.ComponentModel.IBindingListView, System.ComponentModel.ISupportInitializeNotification, System.ComponentModel.ITypedList
public class DataView : System.ComponentModel.MarshalByValueComponent, System.ComponentModel.IBindingListView, System.ComponentModel.ISupportInitializeNotification, System.ComponentModel.ITypedList
type DataView = class
    inherit MarshalByValueComponent
    interface ICollection
    interface IEnumerable
    interface IList
    interface IBindingList
    interface IBindingListView
    interface ISupportInitialize
    interface ISupportInitializeNotification
    interface ITypedList
type DataView = class
    inherit MarshalByValueComponent
    interface IBindingList
    interface IList
    interface ICollection
    interface IEnumerable
    interface ITypedList
    interface ISupportInitialize
type DataView = class
    inherit MarshalByValueComponent
    interface IBindingListView
    interface IBindingList
    interface IList
    interface ICollection
    interface IEnumerable
    interface ITypedList
    interface ISupportInitializeNotification
    interface ISupportInitialize
Public Class DataView
Inherits MarshalByValueComponent
Implements IBindingListView, IList, ISupportInitialize, ISupportInitializeNotification, ITypedList
Public Class DataView
Inherits MarshalByValueComponent
Implements IBindingList, IList, ISupportInitialize, ITypedList
Public Class DataView
Inherits MarshalByValueComponent
Implements IBindingListView, IList, ISupportInitializeNotification, ITypedList
Public Class DataView
Inherits MarshalByValueComponent
Implements IBindingListView, ISupportInitializeNotification, ITypedList
继承
实现

示例

以下示例创建一个包含一 DataTable 列和五行的 。 创建两 DataView 个 对象,并在 RowStateFilter 每个对象上设置 ,以显示表数据的不同视图。 然后输出值。

using System;
using System.Xml;
using System.Data;
using System.Data.Common;
using System.Windows.Forms;

public class Form1: Form
{
    protected DataSet DataSet1;
    protected DataGrid dataGrid1;

    private void DemonstrateDataView()
    {
        // Create one DataTable with one column.
        DataTable table = new DataTable("table");
        DataColumn colItem = new DataColumn("item",
            Type.GetType("System.String"));
        table.Columns.Add(colItem);

        // Add five items.
        DataRow NewRow;
        for(int i = 0; i <5; i++)
        {
            NewRow = table.NewRow();
            NewRow["item"] = "Item " + i;
            table.Rows.Add(NewRow);
        }
        // Change the values in the table.
        table.AcceptChanges();
        table.Rows[0]["item"]="cat";
        table.Rows[1]["item"] = "dog";

        // Create two DataView objects with the same table.
        DataView firstView = new DataView(table);
        DataView secondView = new DataView(table);

        // Print current table values.
        PrintTableOrView(table,"Current Values in Table");

        // Set first DataView to show only modified
        // versions of original rows.
        firstView.RowStateFilter=DataViewRowState.ModifiedOriginal;

        // Print values.
        PrintTableOrView(firstView,"First DataView: ModifiedOriginal");

        // Add one New row to the second view.
        DataRowView rowView;
        rowView=secondView.AddNew();
        rowView["item"] = "fish";

        // Set second DataView to show modified versions of
        // current rows, or New rows.
        secondView.RowStateFilter=DataViewRowState.ModifiedCurrent
            | DataViewRowState.Added;
        // Print modified and Added rows.
        PrintTableOrView(secondView,
            "Second DataView: ModifiedCurrent | Added");
    }

    private void PrintTableOrView(DataTable table, string label)
    {
        // This function prints values in the table or DataView.
        Console.WriteLine("\n" + label);
        for(int i = 0; i<table.Rows.Count;i++)
        {
            Console.WriteLine(table.Rows[i]["item"]);
        }
        Console.WriteLine();
    }

    private void PrintTableOrView(DataView view, string label)
    {

        // This overload prints values in the table or DataView.
        Console.WriteLine("\n" + label);
        for(int i = 0; i<view.Count;i++)
        {
            Console.WriteLine(view[i]["item"]);
        }
        Console.WriteLine();
    }
}
Private Sub DemonstrateDataView()
    ' Create one DataTable with one column.
    Dim table As New DataTable("table")
    Dim colItem As New DataColumn("item", _
        Type.GetType("System.String"))
    table.Columns.Add(colItem)

    ' Add five items.
    Dim NewRow As DataRow
    Dim i As Integer
    For i = 0 To 4
    
    NewRow = table.NewRow()
    NewRow("item") = "Item " & i
    table.Rows.Add(NewRow)
    Next
    table.AcceptChanges()

    ' Create two DataView objects with the same table.
    Dim firstView As New DataView(table)
    Dim secondView As New DataView(table)
    
    ' Change the values in the table.
    table.Rows(0)("item") = "cat"
    table.Rows(1)("item") = "dog"
    
    ' Print current table values.
    PrintTableOrView(table, "Current Values in Table")
        
    ' Set first DataView to show only modified versions of original rows.
    firstView.RowStateFilter = DataViewRowState.ModifiedOriginal

    ' Print values.    
    PrintTableOrView(firstView, "First DataView: ModifiedOriginal")

    ' Add one New row to the second view.
    Dim rowView As DataRowView
    rowView = secondView.AddNew()
    rowView("item") = "fish"
    ' Set second DataView to show modified versions of 
    ' current rows, or New rows.
    secondView.RowStateFilter = DataViewRowState.ModifiedCurrent _
        Or DataViewRowState.Added
    ' Print modified and Added rows.
    PrintTableOrView(secondView, _
        "Second DataView: ModifiedCurrent or Added")
End Sub
    
Overloads Private Sub PrintTableOrView( _
    ByVal view As DataView, ByVal label As String)
    Console.WriteLine(label)
    Dim i As Integer
    For i = 0 To view.count - 1
    
    Console.WriteLine(view(i)("item"))
    Next
    Console.WriteLine()
End Sub
    
Overloads Private Sub PrintTableOrView( _
    ByVal table As DataTable, ByVal label As String)
    Console.WriteLine(label)
    Dim i As Integer
    For i = 0 To table.Rows.Count - 1
    Console.WriteLine(table.Rows(i)("item"))
    Next
    Console.WriteLine()
End Sub

以下示例创建一个 DataView 联机订单,这些订单按 LINQ to DataSet 查询的应付总额排序:

DataTable orders = dataSet.Tables["SalesOrderHeader"];

EnumerableRowCollection<DataRow> query =
    from order in orders.AsEnumerable()
    where order.Field<bool>("OnlineOrderFlag") == true
    orderby order.Field<decimal>("TotalDue")
    select order;

DataView view = query.AsDataView();

bindingSource1.DataSource = view;
Dim orders As DataTable = dataSet.Tables("SalesOrderHeader")

Dim query = _
    From order In orders.AsEnumerable() _
    Where order.Field(Of Boolean)("OnlineOrderFlag") = True _
    Order By order.Field(Of Decimal)("TotalDue") _
    Select order

Dim view As DataView = query.AsDataView()
bindingSource1.DataSource = view

注解

的主要DataView功能是允许在Windows 窗体和Web Forms上进行数据绑定。

此外, DataView 可以自定义 来显示 来自 DataTable的一部分数据。 此功能允许将两个控件绑定到同一 DataTable个 ,但显示不同版本的数据。 例如,一个控件可能绑定到 DataView 显示表中的所有行的 ,另一个控件可能配置为仅显示已从 中删除的 DataTable行。 还 DataTable 具有 DefaultView 属性。 这将返回表的默认值 DataView 。 例如,如果要对表创建自定义视图,请在 返回的 DefaultViewDataView设置 RowFilter

若要创建数据的筛选和排序视图,请 RowFilter 设置 和 Sort 属性。 然后,使用 Item[] 属性返回单个 DataRowView

还可以使用 AddNew 和 方法在行集中添加和 Delete 删除。 使用这些方法时, RowStateFilter 属性可以设置为指定 仅显示 DataView已删除的行或新行。

备注

如果未显式指定 的DataView排序条件,则 DataRowView 中的 DataView 对象将基于 中DataTable.RowsDataRowCollection相应 DataRow DataView 的索引进行排序。

LINQ to DataSet 允许开发人员使用 LINQ 对 DataSet 创建复杂、功能强大的查询。 但是,LINQ to DataSet 查询返回对象的枚举 DataRow ,这在绑定方案中不容易使用。 DataView 可以从 LINQ to DataSet 查询创建,并采用该查询的筛选和排序特征。 LINQ to DataSet 通过提供基于 LINQ 表达式的筛选和排序,扩展了 DataView 的功能,从而进行比基于字符串的筛选和排序更复杂、更强大的筛选和排序操作。 有关详细信息 ,请参阅数据绑定和 LINQ to DataSet

构造函数

DataView()

初始化 DataView 类的新实例。

DataView(DataTable)

使用指定的 DataView 初始化 DataTable 类的新实例。

DataView(DataTable, String, String, DataViewRowState)

使用指定的 DataTableRowFilterSortDataViewRowState 初始化 DataView 类的新实例。

属性

AllowDelete

获取或设置一个值,该值指示是否允许删除。

AllowEdit

获取或设置一个值,该值指示是否允许编辑。

AllowNew

获取或设置一个值,该值指示是否可以通过使用 AddNew() 方法添加新行。

ApplyDefaultSort

获取或设置一个值,该值指示是否使用默认排序。 默认排序是通过 PrimaryKey 指定的所有主键进行排序(升序)。

Container

获取组件的容器。

(继承自 MarshalByValueComponent)
Count

在应用 RowFilterRowStateFilter 之后,获取 DataView 中的记录数。

DataViewManager

获取与此视图关联的 DataViewManager

DesignMode

获取指示组件当前是否处于设计模式的值。

(继承自 MarshalByValueComponent)
Events

获取附加到该组件的事件处理程序的列表。

(继承自 MarshalByValueComponent)
IsInitialized

获取一个值,该值指示组件是否已初始化。

IsOpen

获取一个值,该值指示数据源当前是否打开并投影 DataTable 上的数据视图。

Item[Int32]

从指定的表中获取数据行。

RowFilter

获取或设置用于筛选在 DataView 中查看哪些行的表达式。

RowStateFilter

获取或设置用于 DataView 中的行状态筛选器。

Site

获取或设置组件的站点。

(继承自 MarshalByValueComponent)
Sort

获取或设置 DataView 的一个或多个排序列和排序顺序。

Table

获取或设置源 DataTable

方法

AddNew()

DataView 中添加新行。

BeginInit()

开始初始化在窗体上使用或由另一个组件使用的 DataView。 此初始化在运行时发生。

Close()

关闭 DataView

ColumnCollectionChanged(Object, CollectionChangeEventArgs)

在成功更改 DataColumnCollection 后发生。

CopyTo(Array, Int32)

将项复制到数组中。 仅适用于 Web 窗体界面。

Delete(Int32)

删除指定索引处的行。

Dispose()

释放由 MarshalByValueComponent 使用的所有资源。

(继承自 MarshalByValueComponent)
Dispose(Boolean)

释放由 DataView 对象使用的资源(内存除外)。

EndInit()

结束在窗体上使用或由另一个组件使用的 DataView 的初始化。 此初始化在运行时发生。

Equals(DataView)

确定指定的 DataView 实例是否可视为相等的实例。

Equals(Object)

确定指定对象是否等于当前对象。

(继承自 Object)
Find(Object)

根据指定的排序键值在 DataView 中查找行。

Find(Object[])

根据指定的排序键值在 DataView 中查找行。

FindRows(Object)

返回其列与指定的排序键值匹配的 DataRowView 对象数组。

FindRows(Object[])

返回其列与指定的排序键值匹配的 DataRowView 对象数组。

GetEnumerator()

获取此 DataView 的枚举器。

GetHashCode()

作为默认哈希函数。

(继承自 Object)
GetService(Type)

获取 IServiceProvider 的实施者。

(继承自 MarshalByValueComponent)
GetType()

获取当前实例的 Type

(继承自 Object)
IndexListChanged(Object, ListChangedEventArgs)

在成功更改 DataView 后发生。

MemberwiseClone()

创建当前 Object 的浅表副本。

(继承自 Object)
OnListChanged(ListChangedEventArgs)

引发 ListChanged 事件。

Open()

打开 DataView

Reset()

保留以仅供内部使用。

ToString()

返回包含 Component 的名称的 String(如果有)。 不应重写此方法。

(继承自 MarshalByValueComponent)
ToTable()

基于现有的 DataView 中的行,创建并返回一个新的 DataTable

ToTable(Boolean, String[])

基于现有的 DataView 中的行,创建并返回一个新的 DataTable

ToTable(String)

基于现有的 DataView 中的行,创建并返回一个新的 DataTable

ToTable(String, Boolean, String[])

基于现有的 DataView 中的行,创建并返回一个新的 DataTable

UpdateIndex()

保留以仅供内部使用。

UpdateIndex(Boolean)

保留以仅供内部使用。

事件

Disposed

添加用于侦听组件的 Disposed 事件的事件处理程序。

(继承自 MarshalByValueComponent)
Initialized

DataView 的初始化完成时发生。

ListChanged

更改由 DataView 管理的列表时发生。

显式接口实现

IBindingList.AddIndex(PropertyDescriptor)

有关此成员的说明,请参见 AddIndex(PropertyDescriptor)

IBindingList.AddNew()

有关此成员的说明,请参见 AddNew()

IBindingList.AllowEdit

有关此成员的说明,请参见 AllowEdit

IBindingList.AllowNew

有关此成员的说明,请参见 AllowNew

IBindingList.AllowRemove

有关此成员的说明,请参见 AllowRemove

IBindingList.ApplySort(PropertyDescriptor, ListSortDirection)

有关此成员的说明,请参见 ApplySort(PropertyDescriptor, ListSortDirection)

IBindingList.Find(PropertyDescriptor, Object)

有关此成员的说明,请参见 Find(PropertyDescriptor, Object)

IBindingList.IsSorted

有关此成员的说明,请参见 IsSorted

IBindingList.RemoveIndex(PropertyDescriptor)

有关此成员的说明,请参见 RemoveIndex(PropertyDescriptor)

IBindingList.RemoveSort()

有关此成员的说明,请参见 RemoveSort()

IBindingList.SortDirection

有关此成员的说明,请参见 SortDirection

IBindingList.SortProperty

有关此成员的说明,请参见 SortProperty

IBindingList.SupportsChangeNotification

有关此成员的说明,请参见 SupportsChangeNotification

IBindingList.SupportsSearching

有关此成员的说明,请参见 SupportsSearching

IBindingList.SupportsSorting

有关此成员的说明,请参见 SupportsSorting

IBindingListView.ApplySort(ListSortDescriptionCollection)

有关此成员的说明,请参见 ApplySort(ListSortDescriptionCollection)

IBindingListView.Filter

有关此成员的说明,请参见 Filter

IBindingListView.RemoveFilter()

有关此成员的说明,请参见 RemoveFilter()

IBindingListView.SortDescriptions

有关此成员的说明,请参见 SortDescriptions

IBindingListView.SupportsAdvancedSorting

有关此成员的说明,请参见 SupportsAdvancedSorting

IBindingListView.SupportsFiltering

有关此成员的说明,请参见 SupportsFiltering

ICollection.IsSynchronized

有关此成员的说明,请参见 IsSynchronized

ICollection.SyncRoot

有关此成员的说明,请参见 SyncRoot

IList.Add(Object)

有关此成员的说明,请参见 Add(Object)

IList.Clear()

有关此成员的说明,请参见 Clear()

IList.Contains(Object)

有关此成员的说明,请参见 Contains(Object)

IList.IndexOf(Object)

有关此成员的说明,请参见 IndexOf(Object)

IList.Insert(Int32, Object)

有关此成员的说明,请参见 Insert(Int32, Object)

IList.IsFixedSize

有关此成员的说明,请参见 IsFixedSize

IList.IsReadOnly

有关此成员的说明,请参见 IsReadOnly

IList.Item[Int32]

有关此成员的说明,请参见 Item[Int32]

IList.Remove(Object)

有关此成员的说明,请参见 Remove(Object)

IList.RemoveAt(Int32)

有关此成员的说明,请参见 RemoveAt(Int32)

ITypedList.GetItemProperties(PropertyDescriptor[])

有关此成员的说明,请参见 GetItemProperties(PropertyDescriptor[])

ITypedList.GetListName(PropertyDescriptor[])

有关此成员的说明,请参见 GetListName(PropertyDescriptor[])

扩展方法

Cast<TResult>(IEnumerable)

IEnumerable 的元素强制转换为指定的类型。

OfType<TResult>(IEnumerable)

根据指定类型筛选 IEnumerable 的元素。

AsParallel(IEnumerable)

启用查询的并行化。

AsQueryable(IEnumerable)

IEnumerable 转换为 IQueryable

适用于

线程安全性

此类型对于多线程读取操作是安全的。 必须同步所有写入操作。

另请参阅