通过


DataView 类

定义

表示用于排序、筛选、搜索、编辑和导航的数据绑定自定义视图 DataTable 。 它 DataView 不存储数据,而是表示其相应 DataTable连接的视图。 对DataView数据所做的更改将影响 。DataTableDataTable数据所做的更改将影响与之关联的所有 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 窗体上绑定数据。

此外,还可以自定义 a 以显示来自 <a0/a0> 的数据子集。 此功能使你可以将两个控件绑定到同一 DataTable个控件,但显示不同版本的数据。 例如,一个控件可能绑定到 DataView 显示表中所有行的控件,第二个控件可能配置为仅显示已从中删除的 DataTable行。 DataTable该属性也有一个DefaultView属性。 这会返回表的默认值 DataView 。 例如,如果要在表上创建自定义视图,请设置RowFilter由该表返回的DefaultView视图DataView

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

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

注意

如果未显式指定排序条件DataView,则DataRowView根据 DataView 的DataRowCollectionDataTable.Rows相应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, String, String, DataViewRowState)

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

DataView(DataTable)

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

属性

名称 说明
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()

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

(继承自 MarshalByValueComponent)
ToTable()

根据现有DataView行创建并返回一个新DataTable行。

ToTable(Boolean, String[])

根据现有DataView行创建并返回一个新DataTable行。

ToTable(String, Boolean, String[])

根据现有DataView行创建并返回一个新DataTable行。

ToTable(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[])

扩展方法

名称 说明
AsParallel(IEnumerable)

启用查询的并行化。

AsQueryable(IEnumerable)

IEnumerable 转换为 IQueryable

Cast<TResult>(IEnumerable)

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

OfType<TResult>(IEnumerable)

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

适用于

线程安全性

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

另请参阅