【发布时间】:2017-01-30 04:48:09
【问题描述】:
我创建了一个 DataTable,当我尝试向其中添加一行时,它不需要。我知道DataRow中有信息,但是在foreach循环完成后,我查看DataTable的计数,它显示为0。这是我正在使用的代码:
DataSet CustomColumnsDS = new DataSet();
DataTable dt = new DataTable();
string strXML = GetCatalog(WebUserID, Password); //Web Service Call
XmlDocument doc = new XmlDocument();doc.LoadXml(strXML);
XmlNodeList xnList = doc.SelectNodes("xml/Catalog/item/Package");
if (xnList.Count > 0)//Count = 90
{
dt.Columns.Add("testId", typeof(string));
dt.Columns.Add("testName", typeof(string));
foreach (XmlNode xn in xnList)
{
if (!string.IsNullOrEmpty(xn["Id"].InnerText))
{
DataRow dr = dt.NewRow();
dr["testId"] = xn["Id"].InnerText;
dr["testName"] = xn["Name"].InnerText;
try
{
//At this point the DataRow is filled in with values, but it does not seem to actually add in.
dt.Rows.Add(dr); //No Exception is caught
}
catch (Exception ex)
{
string test = "";
}
}
}
CustomColumnsDS.Tables.Add(dt);//Count = 0;
}
感谢任何帮助。谢谢!
【问题讨论】:
-
是 dt.Rows.Add(dr); 抛出和异常,然后被捕获和吞下?
-
您可以尝试使用 InsertAt(带有计数器变量)dt.Rows.InsertAt(dr, rowCounter);
-
忘记在这里发表评论,没有发现异常。那是我调试的时候用的。
-
如果在添加后计算 DataTable 中的行数:dt.Rows.Add(dr);
-
您从 DataTable 中查看的计数字段是什么?