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 Forms和Web Form上的資料系結。

此外,也可以自訂 來 DataView 呈現 來自 DataTable 的資料子集。 這項功能可讓您將兩個控制項系結至相同的 DataTable ,但顯示不同版本的資料。 例如,一個控制項可能會系結至 DataView ,其中顯示資料表中的所有資料列,而第二個控制項可能會設定為只顯示已從 DataTable 中刪除的資料列。 DataTable也有 DefaultView 屬性。 這會傳回資料表的預設值 DataView 。 例如,如果您想要在資料表上建立自訂檢視,請在 所傳回的 DefaultViewDataView 設定 RowFilter

若要建立已篩選和排序的資料檢視,請設定 RowFilterSort 屬性。 然後,使用 Item[] 屬性傳回單 DataRowView 一 。

您也可以使用 AddNewDelete 方法,從一組資料列新增和刪除。 當您使用這些方法時, RowStateFilter 屬性可以設定為指定只顯示 DataView 已刪除的資料列或新的資料列。

注意

如果您未明確指定 的排序準則 DataView ,中的 DataRowViewDataView 物件會根據 中 DataTable.RowsDataRowCollection 對應的 DataRow 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 類別的新執行個體。

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 Form 介面。

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

適用於

執行緒安全性

此類型適用于多執行緒讀取作業。 您必須同步處理任何寫入作業。

另請參閱