Export (0) Print
Expand All

DataView.Table Property

Gets or sets the source DataTable.

[Visual Basic]
Public Property Table As DataTable
[C#]
public DataTable Table {get; set;}
[C++]
public: __property DataTable* get_Table();
public: __property void set_Table(DataTable*);
[JScript]
public function get Table() : DataTable;
public function set Table(DataTable);

Property Value

A DataTable that provides the data for this view.

Remarks

The DataTable also has a DefaultView property which returns the default DataView for the table. For example, if you wish to create a custom view on the table, set the RowFilter on the DataView returned by the DefaultView.

You can only set the Table property if the current value is null.

Example

[Visual Basic, C#, C++] The following example gets the DataTable of the current DataView.

[Visual Basic] 
Private Sub DemonstrateDataViewTable()
    Dim myTable As DataTable
    myTable = new DataTable()
    
    ' add columns
    Dim column As DataColumn
    column = myTable.Columns.Add( "ProductID",    GetType(Integer) )
    column.AutoIncrement = true
    column = myTable.Columns.Add( "ProductName", GetType(String) )

    ' populate DataTable.
    Dim id As Integer
    For id = 1 To 5
        myTable.Rows.Add( _
            new object() { id, string.Format("product{0}", id) } )
    Next id
    
    Dim dv As DataView
    dv = new DataView( myTable )

    PrintTable(dv.Table, "DataTable")
End Sub

Private Sub PrintTable(t As DataTable, label As String)
    ' This function prints values in the table or DataView.
    Console.WriteLine("\n" + label)
    Dim row As DataRow
    Dim c As DataColumn
    For Each row In t.Rows
        For Each c In t.Columns
            Console.Write( "\t{0}", row(c) )
        Next c
    Next row
    Console.WriteLine()
End Sub

[C#] 
private static void DemonstrateDataViewTable()
{
    DataTable myTable;
    myTable = new DataTable();
    
    // add columns
    DataColumn column;
    column = myTable.Columns.Add( "ProductID",    typeof(int)       );
    column.AutoIncrement = true;
    column = myTable.Columns.Add( "ProductName", typeof(string) );

    // populate DataTable.
    for( int id=1; id<=5; id++ )
    {
        myTable.Rows.Add( 
            new object[]{ id, string.Format("product{0}", id) } );
    }
    
    DataView dv;
    dv = new DataView( myTable );

    PrintTable( dv.Table, "DataTable" );
}

private static void PrintTable(DataTable t, string label)
{
    // This function prints values in the table or DataView.
    Console.WriteLine("\n" + label);
    foreach( DataRow row in t.Rows )
    {
        foreach( DataColumn c in t.Columns )
        {
            Console.Write( "\t{0}", row[c] );
        }
        Console.WriteLine();
    }
}

[C++] 
private:
    static void DemonstrateDataViewTable()
    {
        DataTable* myTable;
        myTable = new DataTable();

        // add columns
        DataColumn* column;
        column = myTable->Columns->Add( S"ProductID",    __typeof(int) );
        column->AutoIncrement = true;
        column = myTable->Columns->Add( S"ProductName", __typeof(String) );

        // populate DataTable.
        for( int id=1; id<=5; id++ )
        {
            Object* temp0 [] = {__box(id), String::Format(S"product{0}", __box(id))};
            myTable->Rows->Add( temp0 );
        }

        DataView* dv;
        dv = new DataView( myTable );

        PrintTable( dv->Table, S"DataTable" );
    }

private:
    static void PrintTable(DataTable* t, String* label)
    {
        // This function prints values in the table or DataView.
        Console::WriteLine(S"\n{0}", label);
        System::Collections::IEnumerator* myEnum = t->Rows->GetEnumerator();
        while (myEnum->MoveNext())
        {
            DataRow* row = __try_cast<DataRow*>(myEnum->Current);
            System::Collections::IEnumerator* myEnum1 = t->Columns->GetEnumerator();
            while (myEnum1->MoveNext())
            {
                DataColumn* c = __try_cast<DataColumn*>(myEnum1->Current);
                Console::Write( S"\t{0}", row->Item[c] );
            }
            Console::WriteLine();
        }
    }

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

See Also

DataView Class | DataView Members | System.Data Namespace | DataTable

Show:
© 2014 Microsoft