(摘录自《C#函数实用手册》冶金工业出版社)
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
DataTable testDT = new DataTable("student");
DataColumn testDC = new DataColumn("ID", Type.GetType("System.Int32"));
testDT.Columns.Add(testDC);
testDC = new DataColumn("Name", Type.GetType("System.String"));
testDT.Columns.Add(testDC);
DataRow testDR;
testDR = testDT.NewRow();
testDR["ID"] = 1;
testDR["Name"] = "Rose";
testDT.Rows.Add(testDR);
testDR = testDT.NewRow();
testDR["ID"] = 2;
testDR["Name"] = "Jock";
testDT.Rows.Add(testDR);
testDT.AcceptChanges();
DataView testDVA = new DataView(testDT);
DataView testDVB = new DataView(testDT);
testDVA.RowStateFilter = DataViewRowState.ModifiedOriginal;
DoPrint(testDVA, "第一个DataView对象(原始数据)");
DataRowView testDRV;
testDRV = testDVB.AddNew();
testDRV["ID"] = 8;
testDRV["Name"] = "Bill";
testDVB.RowStateFilter = DataViewRowState.ModifiedCurrent | DataViewRowState.Added;
DoPrint(testDVB, "第二个DataView对象(修改后数据)");
Console.ReadLine();
}
private static void DoPrint(DataView dv, string str)
{
Console.WriteLine(str);
for (int i = 0; i < dv.Count; i++)
{
Console.WriteLine(" " + dv[i]["id"] + " " + dv[i]["Name"]);
}
Console.WriteLine();
}
}
}
*****************************************
结果:
第一个DataView对象(原始数据)
第二个DataView对象(修改后数据)
8 Bill