class Program { static void Main(string[] args) { DataTable dt = new DataTable(); dt.Columns.Add("ID", typeof(int)); DataRow oneRow = dt.NewRow(); oneRow[0] = 1; Console.WriteLine("新增加时的状态及版本:"); output(oneRow); dt.Rows.Add(oneRow); Console.WriteLine("\n" + "增加到表中后的状态和版本:"); output(oneRow); oneRow.BeginEdit(); oneRow[0] = 5; Console.WriteLine("\n" + "开始编辑时的状态和版本:"); output(oneRow); oneRow.EndEdit(); Console.WriteLine("\n" + "结束编辑时的状态和版本:"); output(oneRow); oneRow.AcceptChanges(); Console.WriteLine("\n" + "接受更改后的状态和版本:"); Console.WriteLine(oneRow[0]); output(oneRow); oneRow[0] = 6; Console.WriteLine("\n" + "再次编辑后的状态和版本"); output(oneRow); dt.Rows[0].Delete(); Console.WriteLine("\n" + "删除后的状态和版本:"); output(oneRow); Console.ReadKey(); } static void output(DataRow oneRow) { int[] version = new int[] { 256,512,1024,1536 }; //打印行状态 Console.WriteLine(oneRow.RowState); foreach (int i in version) { if (oneRow.HasVersion((DataRowVersion)i)) { //打印行版本名称及对应的值,用空格隔开 Console.Write(" " + (DataRowVersion)i + " " + oneRow[0, (DataRowVersion)i]); } } Console.WriteLine("\n-----------------------------------"); } }
运行后的输出:
还是有些闹不明白……