更新DataTable

时间:2014-09-08 10:44:59

标签: c# .net datatable dataset

我试图更新属于DataSet的DataTable。我使用DataRow搜索表,然后将值传递给DataRow以更新到DataSet,但这不起作用,任何人都可以告诉我哪里出错了吗?

foreach(DataRow drHierarchyTrueValueList in ds.Tables["HierarchyTypeValueList"].Rows)
{
  foreach(DataRow drHierarchyListToUpdate in ds.Tables["Hierarchy"].Rows)
  {
    if (drHierarchyListToUpdate["HierarchyParent"] == 
        drHierarchyTrueValueList["HierarchyValueDescription"] && 
        Convert.ToInt32(drHierarchyListToUpdate["HierarchyParentType"]) != 0)
      {
         drHierarchyListToUpdate["HierarchyParentValue"] = 
             Convert.ToInt32(drHierarchyTrueValueList["HierarchyTrueValue"]);
      }
  }
  ds.Tables["Hierarchy"].AcceptChanges();
}  

1 个答案:

答案 0 :(得分:0)

我把它改成了这个,它起作用了:

foreach(DataRow drHierarchyListToUpdate in ds.Tables["Hierarchy"].Rows)
        {
            string HierarchyParent = drHierarchyListToUpdate["HierarchyParent"].ToString();
            string HierarchyValueDescription = drHierarchyTrueValueList["HierarchyValueDescription"].ToString();
            int HierarchyParentType = Convert.ToInt32(drHierarchyListToUpdate["HierarchyParentType"]);

            if (HierarchyParent == HierarchyValueDescription && HierarchyParentType != 0)
            {
                drHierarchyListToUpdate["HierarchyParentValue"] = Convert.ToInt32(drHierarchyTrueValueList["HierarchyTrueValue"]);
            }
            else if (HierarchyParent != HierarchyValueDescription && HierarchyParentType == 0)
            {
                drHierarchyListToUpdate["HierarchyParentValue"] = 0;
            }
        }
        ds.Tables["Hierarchy"].AcceptChanges();