ds.Tables[0].Rows[0].RowState 状态是怎么相互转换的

 DataSet ds=dal.Get();
 此时ds为unchanged   //假设这个时候有两行数据   [0]  [1]
 如果ds.Tables[0].Rows.Add(new DataRow())....此时多了一行为 Added [2]
 
 如果这个时候对数据进行编辑
 比如
 ds.Tables[0].Rows[0]["username"]="zzz";
 ds.Tables[0].Rows[1]["username"]="zzz";
 ds.Tables[0].Rows[2]["username"]="zzz";
 为什么这个时候[0] 和[1] 都变成了Modified,但是[2]还是Added呢?
 
 
 根据下面的分析得出结论:
 
 myTable.NewRow();//Detached 托管的行  不在collection中
 myTable.Rows.Add(myRow);// Added 已经被添加到表中
 myTable.AcceptChanges();//提交上面的更改。。如果不这么做这一步,那么下面再对row修改,已经是添加的行的状态还会是Added
 myRow["FirstName"] = "Scott";//修改Modified
 myRow.Delete();//Deleted
 ======================
           
            DataTable myTable = MakeTable();
            DataRow myRow;

            // Create a new DataRow.
            myRow = myTable.NewRow();
            // Detached row.
            Console.WriteLine("new 了一个新行:myRow = myTable.NewRow(); " + myRow.RowState);

            myTable.Rows.Add(myRow);
            // New row.
            Console.WriteLine("将new的新行添加到collection: myTable.Rows.Add(myRow);" + myRow.RowState);

            myTable.AcceptChanges();
            // Unchanged row.
            Console.WriteLine("提交更改:myTable.AcceptChanges(); " + myRow.RowState);

            myRow["FirstName"] = "Scott";
            // Modified row.
            Console.WriteLine("修改行:myRow[FirstName] = Scott;" + myRow.RowState);

            myRow.Delete();
            // Deleted row.
            Console.WriteLine("删除行: myRow.Delete(); " + myRow.RowState);

你可能感兴趣的:(table)