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 窗体上绑定数据。
此外,还可以自定义 a
若要创建经过筛选和排序的数据视图,请设置 RowFilter 和 Sort 属性。 然后,使用 Item[] 属性返回单个 DataRowView。
还可以使用 AddNew 和 Delete 方法从行集中添加和删除。 使用这些方法时,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) |
使用指定的 DataTable、RowFilter和 SortDataViewRowState.. 初始化类的新实例DataView。 |
| DataView(DataTable) |
属性
| 名称 | 说明 |
|---|---|
| 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 的列表时发生。 |
显式接口实现
扩展方法
| 名称 | 说明 |
|---|---|
| AsParallel(IEnumerable) |
启用查询的并行化。 |
| AsQueryable(IEnumerable) |
将 IEnumerable 转换为 IQueryable。 |
| Cast<TResult>(IEnumerable) |
将 IEnumerable 的元素强制转换为指定类型。 |
| OfType<TResult>(IEnumerable) |
根据指定类型筛选 IEnumerable 的元素。 |
适用于
线程安全性
此类型对于多线程读取操作是安全的。 必须同步任何写入操作。