Share via


DataView에서 DataRowView 컬렉션 쿼리

DataViewDataRowView 개체의 열거할 수 있는 컬렉션을 노출합니다. DataRowViewDataRow의 사용자 지정 뷰를 나타내고 컨트롤에 해당 DataRow의 특정 버전을 표시합니다. DataRow와 같은 컨트롤을 통해서는 DataGridView의 한 버전만 표시할 수 있습니다. DataRowDataRowView 속성을 통해 Row에 의해 노출되는 DataRowView에 액세스할 수 있습니다. DataRowView를 사용하여 값을 보는 경우 RowStateFilter 속성에 따라 원본 DataRow에서 노출되는 행 버전이 결정됩니다. DataRow를 사용하여 다른 행 버전에 액세스하는 방법에 대한 자세한 내용은 행 상태 및 행 버전을 참조하세요. DataView에 의해 노출된 DataRowView 개체의 컬렉션은 열거 가능하므로 LINQ to DataSet를 사용하여 쿼리할 수 있습니다.

다음 예제에서는 Product 테이블에서 색상이 빨강인 제품을 쿼리하고 쿼리 결과로부터 테이블을 만듭니다. 이 테이블에서 DataView를 만들고, 삭제되고 수정된 행을 필터링하도록 RowStateFilter 속성을 설정합니다. 그런 다음 LINQ 쿼리의 소스로 DataView를 사용하고, 수정되고 삭제된 DataRowView 개체를 DataGridView 컨트롤에 바인딩합니다.

DataTable products = _dataSet.Tables["Product"];

// Query for red colored products.
EnumerableRowCollection<DataRow> redProductsQuery =
    from product in products.AsEnumerable()
    where product.Field<string>("Color") == "Red"
    orderby product.Field<decimal>("ListPrice")
    select product;

// Create a table and view from the query.
DataTable redProducts = redProductsQuery.CopyToDataTable();
var view = new DataView(redProducts);

// Mark a row as deleted.
redProducts.Rows[0].Delete();

// Modify product price.
redProducts.Rows[1]["ListPrice"] = 20.00;
redProducts.Rows[2]["ListPrice"] = 30.00;

view.RowStateFilter = DataViewRowState.ModifiedCurrent | DataViewRowState.Deleted;

// Query for the modified and deleted rows.
IEnumerable<DataRowView> modifiedDeletedQuery = from DataRowView rowView in view
                                                select rowView;

dataGridView2.DataSource = modifiedDeletedQuery.ToList();
Dim products As DataTable = dataSet.Tables("Product")

' Query for red colored products.
Dim redProductsQuery = _
From product In products.AsEnumerable() _
Where product.Field(Of String)("Color") = "Red" _
Order By product.Field(Of Decimal)("ListPrice") _
Select product
' Create a table and view from the query.
Dim redProducts As DataTable = redProductsQuery.CopyToDataTable()
Dim view As DataView = New DataView(redProducts)

' Mark a row as deleted.
redProducts.Rows(0).Delete()

' Modify product price.
redProducts.Rows(1)("ListPrice") = 20.0
redProducts.Rows(2)("ListPrice") = 30.0

view.RowStateFilter = DataViewRowState.ModifiedCurrent Or DataViewRowState.Deleted

' Query for the modified and deleted rows.
Dim modifiedDeletedQuery = From rowView As DataRowView In view _
                           Select rowView

dataGridView2.DataSource = modifiedDeletedQuery.ToList()

다음 예제에서는 DataGridView 컨트롤에 바인딩된 뷰에서 제품 테이블을 만듭니다. DataView에서 색상이 빨강인 제품을 쿼리하고 정렬된 결과를 DataGridView 컨트롤에 바인딩합니다.

// Create a table from the bound view representing a query of
// available products.
var view = (DataView)bindingSource1.DataSource;
DataTable productsTable = view.Table;

// Set RowStateFilter to display the current rows.
view.RowStateFilter = DataViewRowState.CurrentRows;

// Query the DataView for red colored products ordered by list price.
var productQuery = from DataRowView rowView in view
                   where rowView.Row.Field<string>("Color") == "Red"
                   orderby rowView.Row.Field<decimal>("ListPrice")
                   select new
                   {
                       Name = rowView.Row.Field<string>("Name"),
                       Color = rowView.Row.Field<string>("Color"),
                       Price = rowView.Row.Field<decimal>("ListPrice")
                   };

// Bind the query results to another DataGridView.
dataGridView2.DataSource = productQuery.ToList();
' Create a table from the bound view representing a query of 
' available products.
Dim view As DataView = CType(bindingSource1.DataSource, DataView)
Dim productsTable As DataTable = CType(view.Table, DataTable)

' Set RowStateFilter to display the current rows.
view.RowStateFilter = DataViewRowState.CurrentRows

' Query the DataView for red colored products ordered by list price.
Dim productQuery = From rowView As DataRowView In view _
                   Where rowView.Row.Field(Of String)("Color") = "Red" _
                   Order By rowView.Row.Field(Of Decimal)("ListPrice") _
                   Select New With {.Name = rowView.Row.Field(Of String)("Name"), _
                                .Color = rowView.Row.Field(Of String)("Color"), _
                                .Price = rowView.Row.Field(Of Decimal)("ListPrice")}

' Bind the query results to another DataGridView.
dataGridView2.DataSource = productQuery.ToList()

참고 항목