Evaluar y enviar comentarios
Contraer todo/Expandir todo Contraer todo
Esta página es específica de
Microsoft Visual Studio 2005/.NET Framework 2.0

Hay además otras versiones disponibles para:
Biblioteca de clases de .NET Framework
DataView (Clase)

Representa una vista personalizada que puede enlazar datos de un DataTable para ordenación, filtrado, búsqueda, edición y exploración.

Espacio de nombres: System.Data
Ensamblado: System.Data (en system.data.dll)

Visual Basic (Declaración)
Public Class DataView
    Inherits MarshalByValueComponent
    Implements IBindingListView, IBindingList, IList, ICollection, _
    IEnumerable, ITypedList, ISupportInitializeNotification, ISupportInitialize
Visual Basic (Uso)
Dim instance As DataView
C#
public class DataView : MarshalByValueComponent, IBindingListView, IBindingList, IList, 
    ICollection, IEnumerable, ITypedList, ISupportInitializeNotification, ISupportInitialize
C++
public ref class DataView : public MarshalByValueComponent, IBindingListView, IBindingList, IList, 
    ICollection, IEnumerable, ITypedList, ISupportInitializeNotification, ISupportInitialize
J#
public class DataView extends MarshalByValueComponent implements IBindingListView, IBindingList, 
    IList, ICollection, IEnumerable, ITypedList, ISupportInitializeNotification, 
    ISupportInitialize
JScript
public class DataView extends MarshalByValueComponent implements IBindingListView, IBindingList, 
    IList, ICollection, IEnumerable, ITypedList, ISupportInitializeNotification, 
    ISupportInitialize

Una función fundamental de DataView es permitir el enlace de datos con formularios Windows Forms y Web Forms.

Además, un DataView se puede personalizar para presentar un subconjunto de datos de DataTable. Esta capacidad permite tener dos controles enlazados al mismo objeto DataTable y que muestren versiones distintas de los datos. Por ejemplo, un control puede estar enlazado a un objeto DataView que muestre todas las filas de la tabla y el segundo puede estar configurado para mostrar sólo las filas que se hayan eliminado de DataTable. La clase DataTable también tiene una propiedad DefaultView. Esta propiedad devuelve el objeto DataView predeterminado para la tabla. Por ejemplo, si desea crear una vista personalizada en la tabla, establezca la propiedad RowFilter en el objeto DataView que devuelve la propiedad DefaultView.

Para crear una vista filtrada y ordenada de datos, establezca las propiedades RowFilter y Sort. A continuación, utilice la propiedad Item para devolver un único DataRowView.

También se pueden agregar y eliminar del conjunto de filas mediante los métodos AddNew y Delete. Cuando se utilizan estos métodos, la propiedad RowStateFilter se puede establecer para que especifique que DataView sólo muestre las filas eliminadas o nuevas.

En el ejemplo siguiente se crea un único DataTable con una columna y cinco filas. Se crean dos objetos DataView y el RowStateFilter se establece en cada uno para mostrar distintas vistas de los datos de la tabla. A continuación, se imprimen los valores.

Visual Basic
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
C#
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();
}

Este tipo es seguro para operaciones de lectura multiproceso. Debe sincronizar cualquier operación de escritura.

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium, Windows Mobile para Pocket PC, Windows Mobile para Smartphone, Windows Server 2003, Windows XP Media Center, Windows XP Professional x64, Windows XP SP2, Windows XP Starter Edition

.NET Framework no admite todas las versiones de cada plataforma. Para obtener una lista de las versiones admitidas, vea Requisitos del sistema.

.NET Framework

Compatible con: 2.0, 1.1, 1.0

.NET Compact Framework

Compatible con: 2.0, 1.0
Contenido de la comunidad   ¿Qué es Community Content?
Agregar contenido nuevo RSS  Anotaciones
Processing
© 2012 Microsoft. Reservados todos los derechos. Términos de uso | Marcas Registradas | Privacidad
Page view tracker