gpt4 book ai didi

c# - 将行值添加到具有 AutoIncreamented DataColumn 的 DataTable

转载 作者:太空狗 更新时间:2023-10-30 00:54:39 24 4
gpt4 key购买 nike

我想在具有自动增量属性的数据列的数据表中添加一行。

DataTable tblproduct = new DataTable();

DataColumn CartItemId = new DataColumn();
CartItemId.ColumnName = "CartItemId";
CartItemId.DataType = System.Type.GetType("System.Int32");
CartItemId.AutoIncrement = true;
CartItemId.AutoIncrementSeed = 1;
CartItemId.AutoIncrementStep = 1;
CartItemId.ReadOnly = true;
CartItemId.Unique = true;

tblproduct.Columns.Add(CartItemId);
tblproduct.Columns.Add("CampaignId", typeof(int));
tblproduct.Columns.Add("SubCatId", typeof(int));
tblproduct.Columns.Add("Size", typeof(string));
tblproduct.Columns.Add("Qty", typeof(int));

tblproduct.Rows.Add(3, 345,"Hello", 1);
tblproduct.Rows.Add(5, 3455,"Hecfghhgdfllo", 8);

我不想在 Autoincreamented 列中插入值。它应该是自动生成的值,但上面的代码对我不起作用。

最佳答案

添加行时,尝试类似的方法

        DataRow dr = tblproduct.NewRow();
dr["CampaignId"] = 3;
dr["SubCatId"] = 345;
dr["Size"] = "Hello";
dr["Qty"] = 1;
tblproduct.Rows.Add(dr);

同样来自 DataColumn.AutoIncrement Property

You can create a new row using the ItemArray property of the DataRow class and passing in an array of values. This is a potential problem for a column with its AutoIncrement set to true, because its value is generated automatically. To use the ItemArray property, place null in the column's position in the array.

将您的代码更改为类似

        tblproduct.Rows.Add(null,3, 345, "Hello", 1);
tblproduct.Rows.Add(null,5, 3455, "Hecfghhgdfllo", 8);

关于c# - 将行值添加到具有 AutoIncreamented DataColumn 的 DataTable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12190487/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com