我使用 DataAdapter
将 DataSet
填充到 SQL CE 数据库。数据显示在绑定(bind)到 DataSet 的 DataTable 的 DataGrid
上。我的数据源中有一个自动递增 ID 字段(或在 SQLCE 中,称为 PRIMARY KEY IDENTITY
);相应地,我还在我的 DataTable
中设置了一个 AutoIncrement ID 列。
/* when DataTable is first populated, start counting from Rows.Count */
/* if empty, starts with 1 */
dt.Column["id"].AutoIncrementSeed = dt.Rows.Count + 1;
当我清除数据表时出现问题。我想将 AutoIncrement Counter 重置回 1 但不能,我尝试了以下操作:
/* clearing and disposing DataTable, DataSet, DataAdaptor does not reset the counter */
dt.Clear();
dt.Dispose();
ds.Clear();
ds.Dispose()
da.Dispose()
/* manually re-setting the AutoIncrementSeed also does not reset the counter */
dt.Column["id"].AutoIncrementSeed = 1;
它刚刚离开了它在 Clear()
之前离开的计数器。如何重置 DataTable 中的 AutoIncrement?
最佳答案
这样使用
dt.Clear();
dt.Column["id"].AutoIncrementStep = -1;
dt.Column["id"].AutoIncrementSeed = -1;
dt.Column["id"].AutoIncrementStep = 1;
dt.Column["id"].AutoIncrementSeed = 1;
查看以下链接了解更多
http://www.eggheadcafe.com/community/aspnet/10/25407/autoincrementseed.aspx
关于c# - 重置数据表中的自动增量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5972699/