Partager via


Interrogation de la collection DataRowView dans un DataView

L’objet DataView expose une collection énumérable d’objets DataRowView. L'objet DataRowView représente une vue personnalisée d'un objet DataRow et affiche une version spécifique de cet objet DataRow dans un contrôle. Une seule version d'un objet DataRow peut être affichée par le biais d'un contrôle, comme un objet DataGridView. Vous pouvez accéder à l'objet DataRow qui est exposé par le DataRowView par le biais de la propriété Row de l'objet DataRowView. Lorsque vous affichez des valeurs à l'aide d'un DataRowView, la propriété RowStateFilter détermine la version de ligne de l'objet DataRow sous-jacent qui est exposée. Pour plus d’informations sur l’accès à différentes versions de lignes à l’aide d’un DataRow, consultez Les états des lignes et les versions de lignes. Étant donné que la collection d’objets DataRowView exposés par l' DataView est énumérable, vous pouvez utiliser LINQ to DataSet pour l’interroger.

L'exemple suivant recherche les produits de couleur rouge dans la table Product et crée une table à partir de cette requête. Un objet DataView est créé à partir de cette table et la propriété RowStateFilter est définie pour filtrer sur les lignes supprimées et modifiées. L'objet DataView est ensuite utilisé comme source dans une requête LINQ, et les objets DataRowView qui ont été modifiés et supprimés sont liés à un contrôle 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()

L'exemple suivant crée une table de produits à partir d'une vue qui est liée à un contrôle DataGridView. Une requête recherche les produits de couleur rouge dans l'objet DataView et les résultats classés sont liés à un contrôle 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()

Voir aussi