Calaio

  博客园 :: 首页 :: 博问 :: 闪存 :: :: :: :: 管理 ::

  詳細內容見:MSDN Home >  .NET Framework >  参考 >  类库 >  System.Data

private void DemonstrateDataView(){
   
// Create one DataTable with one column.
   DataTable myTable = new DataTable("myTable");
   DataColumn colItem 
= new DataColumn("item",Type.GetType("System.String"));
   myTable.Columns.Add(colItem);
   
// Add five items.
   DataRow NewRow;
   
for(int i = 0; i <5; i++){
      NewRow 
= myTable.NewRow();
      NewRow[
"item"= "Item " + i;
      myTable.Rows.Add(NewRow);
   }

   
// Change the values in the table.
   myTable.Rows[0]["item"]="cat";
   myTable.Rows[
1]["item"= "dog";
   myTable.AcceptChanges();

   
// Create two DataView objects with the same table.
   DataView firstView = new DataView(myTable);
   DataView secondView 
= new DataView(myTable);

   
// Print current table values.
   PrintTableOrView(myTable,"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 myDataRowView;
   myDataRowView
=secondView.AddNew();
   myDataRowView[
"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 t, string label){
   
// This function prints values in the table or DataView.
   Console.WriteLine("\n" + label);
   
for(int i = 0; i<t.Rows.Count;i++){
      Console.WriteLine(
"\t" + t.Rows[i]["item"]);
   }

   Console.WriteLine();
}


private void PrintTableOrView(DataView dv, string label){

   
// This overload prints values in the table or DataView.
   Console.WriteLine("\n" + label);
   
for(int i = 0; i<dv.Count;i++){
      Console.WriteLine(
"\t" + dv[i]["item"]);
   }

   Console.WriteLine();
}
posted on 2006-02-10 16:54  Calaio  阅读(684)  评论(0编辑  收藏  举报