private static void DemonstrateDataViewTable ( ) {
DataTable myTable = new DataTable ( );
// add columns
DataColumn 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 = new DataView ( myTable );
PrintTable ( dv.Table, "DataTable" );
}
private static void PrintTable ( DataTable tbl, string label ) {
// This function prints values in the table or DataView.
Response.Write ( label + "<br>" )
foreach ( DataRow row in tbl.Rows ) {
foreach ( DataColumn col in tbl.Columns ) {
Response.Write ( row [ col ] + "<br>" );
}
}
}
Private Sub DemonstrateDataViewTable ( )
Dim myTable As DataTable = new DataTable ( )
' add columns
Dim column As DataColumn = 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 = new DataView ( myTable )
PrintTable ( dv.Table, "DataTable" )
End Sub
Private Sub PrintTable ( tbl As DataTable, label As String )
' This function prints values in the table or DataView.
Response.Write ( label & "<br>" )
Dim row As DataRow
Dim col As DataColumn
For Each row In tbl.Rows
For Each col In tbl.Columns
Response.Write ( row ( col ) & "<br>" )
Next col
Next row
End Sub |