DataRowState
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
namespace DataRowState_Demo
{
class Program
{
static void Main(string[] args)
{
DemonstrateRowState();
}
static private void DemonstrateRowState()
{
// 创建一个仅有一列的空表
DataTable dataTable = new DataTable("dataTable");
DataColumn dataColumn = new DataColumn("dataColumn");
dataTable.Columns.Add(dataColumn);
// 添加十行记录item 0,item 1,item 2,,item 9
DataRow dataRow;
for (int i = 0; i < 10; i++)
{
dataRow = dataTable.NewRow();
dataRow["dataColumn"] = "item " + i;
dataTable.Rows.Add(dataRow);
}
//标记接收数据变化
dataTable.AcceptChanges();
// 创建数据表视图
DataView dataView = new DataView(dataTable);
// 改变一个行值
dataTable.Rows[1]["dataColumn"] = "Hello";
// Add one row:
dataRow = dataTable.NewRow();
dataRow["dataColumn"] = "World";
dataTable.Rows.Add(dataRow);
// 设置行过滤,仅显示新增的和当前改变的
dataView.RowStateFilter = DataViewRowState.Added
| DataViewRowState.ModifiedCurrent;
// 输出行为 "Hello" "World"
PrintView(dataView, "ModifiedCurrent and Added");
// 设置过滤显示被修改行的原始值
dataView.RowStateFilter = DataViewRowState.ModifiedOriginal;
PrintView(dataView, "ModifiedOriginal");
// 删除3行
dataTable.Rows[1].Delete();
dataTable.Rows[2].Delete();
dataTable.Rows[3].Delete();
// 设置行过滤显示只显示和修改
dataView.RowStateFilter = DataViewRowState.Deleted;
PrintView(dataView, "Deleted");
//显示当前数据
dataView.RowStateFilter = DataViewRowState.CurrentRows;
PrintView(dataView, "Current");
//显示没有改变的行
dataView.RowStateFilter = DataViewRowState.Unchanged;
PrintView(dataView, "Unchanged");
//显示原始数据.
dataView.RowStateFilter = DataViewRowState.OriginalRows;
PrintView(dataView, "OriginalRows");
}
static private void PrintView(DataView dataView, string label)
{
Console.WriteLine(" " + label + "---------------");
for (int i = 0; i < dataView.Count; i++)
{
Console.WriteLine(dataView[i]["dataColumn"]);
}
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
namespace DataRowState_Demo
{
class Program
{
static void Main(string[] args)
{
DemonstrateRowState();
}
static private void DemonstrateRowState()
{
// 创建一个仅有一列的空表
DataTable dataTable = new DataTable("dataTable");
DataColumn dataColumn = new DataColumn("dataColumn");
dataTable.Columns.Add(dataColumn);
// 添加十行记录item 0,item 1,item 2,,item 9
DataRow dataRow;
for (int i = 0; i < 10; i++)
{
dataRow = dataTable.NewRow();
dataRow["dataColumn"] = "item " + i;
dataTable.Rows.Add(dataRow);
}
//标记接收数据变化
dataTable.AcceptChanges();
// 创建数据表视图
DataView dataView = new DataView(dataTable);
// 改变一个行值
dataTable.Rows[1]["dataColumn"] = "Hello";
// Add one row:
dataRow = dataTable.NewRow();
dataRow["dataColumn"] = "World";
dataTable.Rows.Add(dataRow);
// 设置行过滤,仅显示新增的和当前改变的
dataView.RowStateFilter = DataViewRowState.Added
| DataViewRowState.ModifiedCurrent;
// 输出行为 "Hello" "World"
PrintView(dataView, "ModifiedCurrent and Added");
// 设置过滤显示被修改行的原始值
dataView.RowStateFilter = DataViewRowState.ModifiedOriginal;
PrintView(dataView, "ModifiedOriginal");
// 删除3行
dataTable.Rows[1].Delete();
dataTable.Rows[2].Delete();
dataTable.Rows[3].Delete();
// 设置行过滤显示只显示和修改
dataView.RowStateFilter = DataViewRowState.Deleted;
PrintView(dataView, "Deleted");
//显示当前数据
dataView.RowStateFilter = DataViewRowState.CurrentRows;
PrintView(dataView, "Current");
//显示没有改变的行
dataView.RowStateFilter = DataViewRowState.Unchanged;
PrintView(dataView, "Unchanged");
//显示原始数据.
dataView.RowStateFilter = DataViewRowState.OriginalRows;
PrintView(dataView, "OriginalRows");
}
static private void PrintView(DataView dataView, string label)
{
Console.WriteLine(" " + label + "---------------");
for (int i = 0; i < dataView.Count; i++)
{
Console.WriteLine(dataView[i]["dataColumn"]);
}
}
}
}