1

我认为这会相当容易,但我在这里......我试图简单地向现有行添加一个新行, DataTable但表格不会占用我的行......

Debug.Print("My Achv Count Before: {0}", Manager.achievementsMine.Rows.Count);
DataRow newRow = Manager.achievementsMine.NewRow();
newRow.SetField<int>("CT_Q", count);
newRow.SetField<DateTime>("CMPL_D", DateTime.Now);
newRow.SetField<string>("USER_LAN_I", Manager.userID);
newRow.SetField<string>("ACHV_I", ACHV_I);
Manager.achievementsMine.ImportRow(newRow);
Manager.achievementsMine.AcceptChanges();
Debug.Print("My Achv Count After: {0}", Manager.achievementsMine.Rows.Count);

'Debug.Print'之前/之后的两条线都显示我的计数为44。为什么'after'调试不告诉我45,因为我刚刚导入了一个新行并告诉我DataTable接受更改?

编辑:我尝试重写一些newRow部分但没有改变:

Debug.Print("My Achv Count Before: {0}", Manager.achievementsMine.Rows.Count);
DataRow newRow = Manager.achievementsMine.NewRow();
newRow["CT_Q"] = count;
newRow["CMPL_D"] = DateTime.Now;
newRow["USER_LAN_I"] = Manager.userID;
newRow["ACHV_I"] = ACHV_I;
Manager.achievementsMine.ImportRow(newRow);
Manager.achievementsMine.AcceptChanges();
Debug.Print("My Achv Count After: {0}", Manager.achievementsMine.Rows.Count);
4

1 回答 1

6

如果你想使用NewRow你必须稍后到表AddDataRow

DataRow newRow = Manager.achievementsMine.NewRow();
newRow.SetField<int>("CT_Q", count);
newRow.SetField<DateTime>("CMPL_D", DateTime.Now);
newRow.SetField<string>("USER_LAN_I", Manager.userID);
newRow.SetField<string>("ACHV_I", ACHV_I);
Manager.achievementsMine.Rows.Add(newRow);

另一种方法是使用DataRowColection.Add

DataRow newRow = Manager.achievementsMine.Rows.Add();
// you don't need to add this row since it's already added.

注意AcceptChanges:如果您想稍后将新行插入数据库,请不要调用。AcceptChanges会将其更改RowState为未更改,因此 aDataAdapter无法检测到该行是新行。DataAdapter本身会在Update.

于 2013-11-05T16:02:32.090 回答