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 Forms 和 Web Form 上的數據系結。
此外,也可以自定義 來 DataView 呈現 來自 DataTable的數據子集。 這項功能可讓您有兩個控件系結至相同的 DataTable,但顯示不同版本的數據。 例如,一個控件可能會系結至 DataView ,其中顯示數據表中的所有數據列,而第二個控件可能會設定為只顯示已從 DataTable中刪除的數據列。 DataTable也有 DefaultView 屬性。 這會傳回數據表的預設值 DataView 。 例如,如果您要在資料表上建立自訂檢視,請在 所傳回的 DefaultView上DataView設定 RowFilter 。
若要建立已篩選和排序的數據檢視,請設定 RowFilter 和 Sort 屬性。 然後,使用 Item[] 屬性傳回單 DataRowView一 。
您也可以使用 AddNew 和 Delete 方法,從數據列集新增和刪除。 當您使用這些方法時, RowStateFilter 屬性可以設定為指定只顯示 DataView已刪除的數據列或新的數據列。
注意
如果您未明確指定 的排序準則,中的物件會根據 中對應的 DataTable.Rows
DataRowCollection
DataRow
DataView 索引進行排序。DataView
DataRowView
DataView
LINQ to DataSet 可讓開發人員使用 LINQ 建立複雜且功能強大的查詢DataSet。 不過,LINQ to DataSet 查詢會傳回物件的列舉DataRow,這在系結案例中並不容易使用。 DataView可以從 LINQ to DataSet 查詢建立,並接受該查詢的篩選和排序特性。 LINQ to DataSet 擴充了 DataView 的功能,因為其提供 LINQ 運算式的篩選及排序功能,如此便能完成比字串類篩選和排序更複雜、更強大的篩選和排序作業。 如需詳細資訊,請參閱數據系結和 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 問題作為內容的意見反應機制,並將它取代為新的意見反應系統。 如需詳細資訊,請參閱:提交並檢視相關的意見反應