DataTableExtensions.AsDataView Method

Definition

Overloads

AsDataView(DataTable)

Creates and returns a LINQ-enabled DataView object.

AsDataView<T>(EnumerableRowCollection<T>)

Creates and returns a LINQ-enabled DataView object representing the LINQ to DataSet query.

AsDataView(DataTable)

Source:
DataTableExtensions.cs
Source:
DataTableExtensions.cs
Source:
DataTableExtensions.cs

Creates and returns a LINQ-enabled DataView object.

C#
public static System.Data.DataView AsDataView(this System.Data.DataTable table);

Parameters

table
DataTable

The source DataTable from which the LINQ-enabled DataView is created.

Returns

A LINQ-enabled DataView object.

Examples

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:

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

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

dataGridView1.AutoResizeColumns();

Remarks

DataView enables data-binding scenarios for LINQ to DataSet and can be created from a typed or untyped DataTable, providing a default view of that table. Filtering and sorting can be set on the DataView after it has been created from a DataTable. The DataView is then bound to a UI control, such as a DataGrid or a DataGridView, providing a simple data binding model.

For more information and examples, see Creating a DataView Object.

Applies to

.NET 9 and other versions
Product Versions
.NET Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.1

AsDataView<T>(EnumerableRowCollection<T>)

Source:
DataTableExtensions.cs
Source:
DataTableExtensions.cs
Source:
DataTableExtensions.cs

Creates and returns a LINQ-enabled DataView object representing the LINQ to DataSet query.

C#
public static System.Data.DataView AsDataView<T>(this System.Data.EnumerableRowCollection<T> source) where T : System.Data.DataRow;

Type Parameters

T

The type of objects in the source sequence, typically DataRow.

Parameters

source
EnumerableRowCollection<T>

The source LINQ to DataSet query from which the LINQ-enabled DataView is created.

Returns

A LINQ-enabled DataView object.

Examples

The following example creates a DataView of online orders ordered by total due:

C#
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;

Remarks

DataView enables data binding scenarios for LINQ to DataSet and can be created from a LINQ to DataSet query. The DataView represents the query itself, and is not a view on top of the query. The newly created DataView infers the filtering and sorting information from the query it is created from. The DataView is then bound to a UI control, such as a DataGrid or a DataGridView, providing a simple data-binding model.

The parameter T of the input parameter source can only be of type DataRow or a type derived from DataRow.

The following query operators, only, are supported in a query used to create DataView:

For more information and examples, see Creating a DataView Object.

Applies to

.NET 9 and other versions
Product Versions
.NET Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.1