DataView 类
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
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 。 例如,如果要对表创建自定义视图,请在 返回的 DefaultView上DataView设置 RowFilter 。
若要创建数据的筛选和排序视图,请 RowFilter 设置 和 Sort 属性。 然后,使用 Item[] 属性返回单个 DataRowView。
还可以使用 AddNew 和 方法在行集中添加和 Delete 删除。 使用这些方法时, RowStateFilter 属性可以设置为指定 仅显示 DataView已删除的行或新行。
注意
如果未显式指定 的DataView
排序条件,则 DataRowView
中的 DataView
对象将基于 中DataTable.Rows
DataRowCollection
相应 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, String, String, DataViewRowState) |
使用指定的 DataTable、RowFilter、Sort 和 DataViewRowState 初始化 DataView 类的新实例。 |
属性
AllowDelete |
获取或设置一个值,该值指示是否允许删除。 |
AllowEdit |
获取或设置一个值,该值指示是否允许编辑。 |
AllowNew |
获取或设置一个值,该值指示是否可以通过使用 AddNew() 方法添加新行。 |
ApplyDefaultSort |
获取或设置一个值,该值指示是否使用默认排序。 默认排序是通过 PrimaryKey 指定的所有主键进行排序(升序)。 |
Container |
获取组件的容器。 (继承自 MarshalByValueComponent) |
Count |
在应用 RowFilter 和 RowStateFilter 之后,获取 DataView 中的记录数。 |
DataViewManager |
获取与此视图关联的 DataViewManager。 |
DesignMode |
获取指示组件当前是否处于设计模式的值。 (继承自 MarshalByValueComponent) |
Events |
获取附加到该组件的事件处理程序的列表。 (继承自 MarshalByValueComponent) |
IsInitialized |
获取一个值,该值指示组件是否已初始化。 |
IsOpen |
获取一个值,该值指示数据源当前是否打开并投影 DataTable 上的数据视图。 |
Item[Int32] |
从指定的表中获取数据行。 |
RowFilter |
获取或设置用于筛选在 DataView 中查看哪些行的表达式。 |
RowStateFilter |
获取或设置用于 DataView 中的行状态筛选器。 |
Site |
获取或设置组件的站点。 (继承自 MarshalByValueComponent) |
Sort |
获取或设置 DataView 的一个或多个排序列和排序顺序。 |
Table |
获取或设置源 DataTable。 |
方法
事件
Disposed |
添加用于侦听组件的 Disposed 事件的事件处理程序。 (继承自 MarshalByValueComponent) |
Initialized |
DataView 的初始化完成时发生。 |
ListChanged |
更改由 DataView 管理的列表时发生。 |
显式接口实现
扩展方法
适用于
线程安全性
此类型对于多线程读取操作是安全的。 必须同步所有写入操作。
另请参阅
反馈
https://aka.ms/ContentUserFeedback。
即将发布:在整个 2024 年,我们将逐步淘汰作为内容反馈机制的“GitHub 问题”,并将其取代为新的反馈系统。 有关详细信息,请参阅:提交和查看相关反馈