DataView 成员 : 表示用于排序、筛选、搜索、编辑和导航的 DataTable 的可绑定数据的自定义视图。(转)已修正
Posted on 2008-09-01 18:15 Ran 阅读(498) 评论(0) 收藏 举报//DataView 成员 : 表示用于排序、筛选、搜索、编辑和导航的 DataTable 的可绑定数据的自定义视图。
private void DemonstrateDataView()
{
// Create one DataTable with two column.
DataTable table = new DataTable("table");
DataColumn colItem = new DataColumn("Animal", Type.GetType("System.String"));
table.Columns.Add(colItem);
table.Columns.Add(new DataColumn("Age", typeof(string)));//两种方式加项目
// Add five rows.
DataRow NewRow;
for (int i = 0; i < 5; i++)
{
NewRow = table.NewRow();
NewRow["Animal"] = "Row" + i;
table.Rows.Add(NewRow);
}
//结果:
//Animal Age
//Row0
//Row1
//Row2
//Row3
//Row4
//----------------------------------------------------------------
// Change the values in the table.
table.Rows[0]["Animal"] = "Row_cat";
table.Rows[1]["Animal"] = "Row_dog";
table.AcceptChanges();
//结果:
//Animal Age
//Row_cat
//Row_dog
//Row2
//Row3
//Row4
//----------------------------------------------------------------
// Create two DataView objects with the same table.
DataView firstView = new DataView(table);
//// Set the RowStateFilter to display only Added and modified rows.
//firstView.RowStateFilter = DataViewRowState.ModifiedOriginal;
//结果:
//Animal Age
//----------------------------------------------------------------
// Delete three rows.
table.Rows[1].Delete();
table.Rows[2].Delete();
table.Rows[3].Delete();
//结果:
//Animal Age
//Row_cat
//Row4
//----------------------------------------------------------------
//// Set first DataView to show only modified versions of original rows.
//firstView.RowStateFilter = DataViewRowState.Deleted; //DataViewRowState.Deleted表示只显示被删除了的行
//结果:
//Animal Age
//Row_dog
//Row2
//Row3
//----------------------------------------------------------------
//// Set second DataView to show modified versions of current rows, or New rows.
//firstView.RowStateFilter = DataViewRowState.ModifiedCurrent | DataViewRowState.Added;
//结果:
//Animal Age
//----------------------------------------------------------------
//firstView.RowFilter = "Animal='Row_cat'"; //过滤行:显示项目Animal的行值为"'Row_cat'"
//结果:
//Animal Age
//Row_cat
//----------------------------------------------------------------
dataGrid1.DataSource = firstView;
dataGrid1.DataBind();
}
浙公网安备 33010602011771号