gpt4 book ai didi

c# - DataTable 覆盖上一行

转载 作者:太空宇宙 更新时间:2023-11-03 12:36:34 26 4
gpt4 key购买 nike

因此,我尝试向 DataTable 添加一些行,但由于某种原因,它似乎覆盖了前一行。我该如何防止这种情况。

到目前为止我所拥有的是这段代码:

DataTable table = new DataTable();

foreach (CallData data in p.ValueArray)
{
DataRow row = table.NewRow();
foreach (CallParm parm in data.Parm)
{
if (!table.Columns.Contains(parm.Name))
{
table.Columns.Add(parm.Name, typeof(string));
}
row[parm.Name] = parm.Value;
}
table.Rows.Add(row);
}

数据示例:

{
ValueArray: [
{
Parm: [
{
Name: "ID",
Value: 1
},
{
Name: "ID",
Value: 2
},
{
Name: "ID",
Value: 4
}
]
}
]
}

数据结果

"ID": 4

想要的结果

"ID": 1
"ID": 2
"ID": 4

我已经检查过它是否按预期运行循环,没有问题。当我们添加行时,问题似乎发生了

最佳答案

您需要添加 Param 中的所有值,而不仅仅是最后一个。你可以这样试试

 DataTable table = new DataTable();

foreach (CallData data in p.ValueArray)
{
DataRow row = table.NewRow();
foreach (CallParm parm in data.Parm)
{
if (!table.Columns.Contains(parm.Name))
{
table.Columns.Add(parm.Name, typeof(string));
}
row[parm.Name] = parm.Value;
//now you add all items in this loop not only last element.
table.Rows.Add(row);
}

}

关于c# - DataTable 覆盖上一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40759144/

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