Assembly : System.Data (dans system.data.dll)
SyntaxePublic Class DataView Inherits MarshalByValueComponent Implements IBindingListView, IBindingList, IList, ICollection, _ IEnumerable, ITypedList, ISupportInitializeNotification, ISupportInitialize
Dim instance As DataView
public class DataView : MarshalByValueComponent, IBindingListView, IBindingList, IList, ICollection, IEnumerable, ITypedList, ISupportInitializeNotification, ISupportInitialize
public ref class DataView : public MarshalByValueComponent, IBindingListView, IBindingList, IList, ICollection, IEnumerable, ITypedList, ISupportInitializeNotification, ISupportInitialize
public class DataView extends MarshalByValueComponent implements IBindingListView, IBindingList, IList, ICollection, IEnumerable, ITypedList, ISupportInitializeNotification, ISupportInitialize
public class DataView extends MarshalByValueComponent implements IBindingListView, IBindingList, IList, ICollection, IEnumerable, ITypedList, ISupportInitializeNotification, ISupportInitialize
NotesUne fonction principale de DataView consiste à autoriser la liaison de données sur les Windows Forms et les Web Forms.
En outre, DataView peut être personnalisé pour présenter un sous-jeu de données à partir de DataTable. Cette fonctionnalité vous permet d'avoir deux contrôles liés au même DataTable, mais affichant des versions différentes des données. Par exemple, un contrôle peut être lié à un DataView qui affiche toutes les lignes de la table et un deuxième peut être configuré pour afficher uniquement les lignes ayant été supprimées de DataTable. DataTable a également une propriété DefaultView. Ceci retourne le DataView par défaut pour la table. Par exemple, si vous souhaitez créer une vue personnalisée sur la table, affectez comme valeur de RowFilter le DataView retourné par DefaultView.
Pour créer une vue filtrée et triée des données, définissez les propriétés RowFilter et Sort. Utilisez ensuite la propriété Item pour retourner un DataRowView unique.
Vous pouvez également ajouter et supprimer des éléments du jeu de lignes à l'aide des méthodes AddNew et Delete. Lorsque vous utilisez ces méthodes, la propriété RowStateFilter peut être définie pour spécifier que seules les lignes supprimées ou les nouvelles lignes doivent être affichées par DataView.
ExempleL'exemple suivant crée un DataTable unique avec une colonne et cinq lignes. Deux objets DataView sont créés et RowStateFilter est défini sur chacun pour afficher différentes vues des données de la table. Les valeurs s'affichent ensuite.
Private Sub DemonstrateDataView() ' Create one DataTable with one column. Dim table As DataTable = New DataTable("table") Dim colItem As DataColumn = 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 DataView = New DataView(table) Dim secondView As DataView = 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
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.Rows[0]["item"]="cat"; table.Rows[1]["item"] = "dog"; table.AcceptChanges(); // 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" + 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("\table" + view[i]["item"]); } Console.WriteLine(); }
Sécurité des threadsCe type est sécurisé pour les opérations de lecture multithread. Vous devez synchroniser les opérations d'écriture.
Plates-formesWindows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile pour Pocket PC, Windows Mobile pour Smartphone, Windows Server 2003, Windows XP Édition Media Center, Windows XP Professionnel Édition x64, Windows XP SP2, Windows XP Starter Edition
Le .NET Framework ne prend pas en charge toutes les versions de chaque plate-forme. Pour obtenir la liste des versions prises en charge, consultez Configuration requise.
Informations de version
Outils (masquer)
S'enregistrer
Liste des Membres
Qui est en ligne?
FAQ