none
Add nodes to treeview from a datateble error on relationship RRS feed

  • Question

  • PLS HELP IM Dieing over here!!


    this is the error i get :This constraint cannot be enabled as not all values have corresponding parent value

    public void Load_tree(TreeView treeView,DataTable dt)
           {

              
               DataSet ds = new DataSet();
               ds.Tables.Add(dt);

               DataColumn parentColumn = new DataColumn("NodeId");
               DataColumn childColumn = new DataColumn("ParentId");
              
               parentColumn = ds.Tables[0].Columns["Nodeid"];
               childColumn = ds.Tables[0].Columns["NodeParent"];

     I GET ERROR HERE:
               ds.Relations.Add("rsParentChild", parentColumn, childColumn);
              
              
               treeView.Nodes.Clear();

               foreach (DataRow dr in ds.Tables[0].Rows)
               {
                   if (dr["ParentId"].ToString() == "0")
                    {
                        TreeNode root = new TreeNode(dr["NodeText"].ToString());
                        root.Tag = dr["NodeId"].ToString();
                        treeView.Nodes.Add(root);

                        PopulateTree(dr, root);
                    }
                }
                treeView.ExpandAll();
            }

           public void PopulateTree(DataRow dr, TreeNode pNode)
           {
               foreach (DataRow row in dr.GetChildRows("rsParentChild"))
               {
                   TreeNode cChild = new TreeNode(row["NodeText"].ToString());
                   cChild.Tag = row["NodeId"].ToString();
                   pNode.Nodes.Add(cChild);
                   //Recursively build the tree
                   PopulateTree(row, cChild);
               }
           }
    McC
    Wednesday, June 17, 2009 5:19 PM

Answers

  • You could consider creating a DataRelation without a ForeignKeyConstraint using the createConstraints Boolean parameter.

    ds.Relations.Add(new DataRelation("rsParentChild", parentColumn, childColumn, false));


    Wednesday, June 17, 2009 11:24 PM

All replies

  • You could consider creating a DataRelation without a ForeignKeyConstraint using the createConstraints Boolean parameter.

    ds.Relations.Add(new DataRelation("rsParentChild", parentColumn, childColumn, false));


    Wednesday, June 17, 2009 11:24 PM
  • Also another thing to consider is to review the data that you have in the tables. Clearly there is data missing which is why the constraint cannot be enabled in the first place.

    Thanks,
    Chris Robinson
    Program Manager - DataSet
    This posting is provided "AS IS" with no warranties, and confers no rights.
    Thursday, June 18, 2009 3:46 PM