DataTableExtensions.AsDataView 方法

定義

多載

AsDataView(DataTable)

建立並傳回已啟用 LINQ 的 DataView 物件。Creates and returns a LINQ-enabled DataView object.

AsDataView<T>(EnumerableRowCollection<T>)

建立並傳回已啟用 LINQ 的 DataView 物件,表示 LINQ to DataSetLINQ to DataSet 查詢。Creates and returns a LINQ-enabled DataView object representing the LINQ to DataSetLINQ to DataSet query.

AsDataView(DataTable)

建立並傳回已啟用 LINQ 的 DataView 物件。Creates and returns a LINQ-enabled DataView object.

public:
[System::Runtime::CompilerServices::Extension]
 static System::Data::DataView ^ AsDataView(System::Data::DataTable ^ table);
public static System.Data.DataView AsDataView (this System.Data.DataTable table);
static member AsDataView : System.Data.DataTable -> System.Data.DataView
<Extension()>
Public Function AsDataView (table As DataTable) As DataView

參數

table
DataTable

來源 DataTable,已啟用 LINQ 的 DataView 是從其中建立。The source DataTable from which the LINQ-enabled DataView is created.

傳回

已啟用 LINQ 的 DataView 物件。A LINQ-enabled DataView object.

範例

下列範例會從 SalesOrderDetail 資料表建立 DataView,並將它設定為 @no__t 1 物件的資料來源,作為 @no__t 2 控制項的 proxy:The following example creates a DataView from the SalesOrderDetail table and sets it as the data source of a BindingSource object, which acts as a proxy for a DataGridView control:

DataTable orders = dataSet.Tables["SalesOrderDetail"];

DataView view = orders.AsDataView();
bindingSource1.DataSource = view;

dataGridView1.AutoResizeColumns();
Dim orders As DataTable = dataSet.Tables("SalesOrderDetail")

Dim view As DataView = orders.AsDataView()
bindingSource1.DataSource = view
dataGridView1.AutoResizeColumns()

備註

DataView 會啟用 LINQ to DataSetLINQ to DataSet 的資料系結案例,而且可以從具型別或不具類型的 DataTable 建立,提供該資料表的預設視圖。DataView enables data-binding scenarios for LINQ to DataSetLINQ to DataSet and can be created from a typed or untyped DataTable, providing a default view of that table. 在您已經從 DataView 中建立 DataTable 之後,就可以針對它設定篩選和排序。Filtering and sorting can be set on the DataView after it has been created from a DataTable. @No__t-0 接著會系結至 UI 控制項,例如 DataGridDataGridView,提供簡單的資料系結模型。The DataView is then bound to a UI control, such as a DataGrid or a DataGridView, providing a simple data binding model.

如需詳細資訊和範例,請參閱建立 DataView 物件For more information and examples, see Creating a DataView Object.

AsDataView<T>(EnumerableRowCollection<T>)

建立並傳回已啟用 LINQ 的 DataView 物件,表示 LINQ to DataSetLINQ to DataSet 查詢。Creates and returns a LINQ-enabled DataView object representing the LINQ to DataSetLINQ to DataSet query.

public:
generic <typename T>
 where T : System::Data::DataRow[System::Runtime::CompilerServices::Extension]
 static System::Data::DataView ^ AsDataView(System::Data::EnumerableRowCollection<T> ^ source);
public static System.Data.DataView AsDataView<T> (this System.Data.EnumerableRowCollection<T> source) where T : System.Data.DataRow;
static member AsDataView : System.Data.EnumerableRowCollection<'T (requires 'T :> System.Data.DataRow)> -> System.Data.DataView (requires 'T :> System.Data.DataRow)
<Extension()>
Public Function AsDataView(Of T As DataRow) (source As EnumerableRowCollection(Of T)) As DataView

類型參數

T

來源序列中的物件類型,通常是 DataRowThe type of objects in the source sequence, typically DataRow.

參數

source
EnumerableRowCollection<T>

來源 LINQ to DataSetLINQ to DataSet 查詢,已啟用 LINQ 的 DataView 是從其中建立。The source LINQ to DataSetLINQ to DataSet query from which the LINQ-enabled DataView is created.

傳回

已啟用 LINQ 的 DataView 物件。A LINQ-enabled DataView object.

範例

下列範例會建立依總到期時間排序的 DataView 個線上訂單:The following example creates a DataView of online orders ordered by total due:

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 會啟用 LINQ to DataSetLINQ to DataSet 的資料系結案例,而且可以從 @no__t 2 查詢建立。DataView enables data binding scenarios for LINQ to DataSetLINQ to DataSet and can be created from a LINQ to DataSetLINQ to DataSet query. @No__t-0 代表查詢本身,而不是查詢頂端的視圖。The DataView represents the query itself, and is not a view on top of the query. 新建立的 DataView 會從所建立的查詢推斷篩選和排序資訊。The newly created DataView infers the filtering and sorting information from the query it is created from. @No__t-0 接著會系結至 UI 控制項,例如 DataGrid 或 @no__t 2,提供簡單的資料系結模型。The DataView is then bound to a UI control, such as a DataGrid or a DataGridView, providing a simple data-binding model.

輸入參數 T 的參數 source 只能屬於類型 DataRow 或衍生自 DataRow 的類型。The parameter T of the input parameter source can only be of type DataRow or a type derived from DataRow.

下列查詢運算子僅在用來建立 DataView 的查詢中受到支援:The following query operators, only, are supported in a query used to create DataView:

如需詳細資訊和範例,請參閱建立 DataView 物件For more information and examples, see Creating a DataView Object.

適用於