在DataTable中重置自动增量 [英] Reset AutoIncrement in DataTable
问题描述
DataSet
与 DataAdapter
填充到 SQL CE 数据库。数据显示在绑定到DataSet的DataTable的 DataGrid
上。我的DataSource中有一个自动增量ID字段(或SQLCE,称为 PRIMARY KEY IDENTITY
);相应地,我还在我的 DataTable
中设置了一个AutoIncrement ID列。 / *当第一次填充DataTable时,从Rows.Count * /
/ *开始计数,如果为空,则以1 * /
dt.Column [id]开头。AutoIncrementSeed = dt.Rows.Count + 1;
当我清除DataTable时出现问题。我想将AutoIncrement Counter重新设置为1但不能,我尝试以下操作:
/ *清除和处理DataTable ,DataSet,DataAdaptor不会重置计数器* /
dt.Clear();
dt.Dispose();
ds.Clear();
ds.Dispose()
da.Dispose()
/ *手动重新设置AutoIncrementSeed也不会重置计数器* /
dt.Column [ id]。AutoIncrementSeed = 1;
它只剩下计数器,然后再在 Clear() code>。如何重新设置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
I populate a DataSet
with a DataAdapter
to a SQL CE Database. Data is displayed on a DataGrid
which is binded to DataSet's DataTable. I have an auto-increment ID field (or in SQLCE, called PRIMARY KEY IDENTITY
) in my DataSource; correspondingly, I also set a AutoIncrement ID column in my DataTable
.
/* when DataTable is first populated, start counting from Rows.Count */
/* if empty, starts with 1 */
dt.Column["id"].AutoIncrementSeed = dt.Rows.Count + 1;
Problem come when I clear my DataTable. I want to reset the AutoIncrement Counter back to 1 but not able to, I tried the following:
/* 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;
It just left with the counter it left off before Clear()
. How can I reset the AutoIncrement in the DataTable?
Use like this
dt.Clear();
dt.Column["id"].AutoIncrementStep = -1;
dt.Column["id"].AutoIncrementSeed = -1;
dt.Column["id"].AutoIncrementStep = 1;
dt.Column["id"].AutoIncrementSeed = 1;
Check the below link for more
http://www.eggheadcafe.com/community/aspnet/10/25407/autoincrementseed.aspx
这篇关于在DataTable中重置自动增量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!