How to use treeview-dynamica put nodes values?

Last post 07-16-2009 1:02 PM by bjcanada. 5 replies.

Sort Posts:

  • How to use treeview-dynamica put nodes values?

    03-09-2009, 4:56 PM
    • Member
      8 point Member
    • bjcanada
    • Member since 06-17-2008, 5:12 PM
    • Posts 108

    Hi,

     I need use treeview to show the Hierarchical relationship, all the data information save the in one table.

    How can I find the data from table and put to the treeview with Hierarchical relationship?

     

    Thanks in advance!

     

  • Re: How to use treeview-dynamica put nodes values?

    03-09-2009, 5:19 PM
    • All-Star
      92,403 point All-Star
    • SGWellens
    • Member since 01-02-2007, 4:27 PM
    • Twin Cities, MN
    • Posts 7,533
    • Moderator
      TrustedFriends-MVPs

    I wrote a post about that very subject on my blog:

    Here

    Steve Wellens

    My blog
  • Re: How to use treeview-dynamica put nodes values?

    03-11-2009, 3:45 PM
    • Member
      8 point Member
    • bjcanada
    • Member since 06-17-2008, 5:12 PM
    • Posts 108

    Thanks a lot for you reply

  • Re: How to use treeview-dynamica put nodes values?

    03-11-2009, 3:48 PM
    Answer
    • Member
      8 point Member
    • bjcanada
    • Member since 06-17-2008, 5:12 PM
    • Posts 108

    I got another solution for that, not use the xml file. The code is below.

    trvGroup is name of treeview control  

    protected void LoadTree(int clientID)

    {

    int id;

    int parent;

    string keyword;

    TreeNode node = new TreeNode();

    BLCommons blc = new BLCommons();

    TreeNode root = new TreeNode("Root");

    root.Value = "0";

    trvGroup.Nodes.Clear();

    trvGroup.Nodes.Add(root);

     

    string str = "Select GroupID,Title,isnull(ParentGroupID,0) From ConferenceGroup" +

    " where ConferenceGroup.ClientID = '" + clientID + "' order by ParentGroupID ";

    SqlDataReader get = blc.BLCommonsGetDataAllSTP(str);while (get.Read())

    {

    TreeNode child = new TreeNode();

    id = get.GetInt32(0);

    keyword = get.GetString(1);

    parent = get.GetInt32(2);

    child.Text = keyword;

    child.Value = id.ToString();

    if (parent == 0)

    {

    trvGroup.Nodes[0].ChildNodes.Add(child);

    trvGroup.Nodes[0].SelectAction =
    TreeNodeSelectAction.Expand;

    }

    else if ((null != node) && (string.Compare(node.Value, parent.ToString()) == 0))

    {

    node.ChildNodes.Add(child);

    node.SelectAction =
    TreeNodeSelectAction.Expand;

    }

    else

    {

    node = Find_Node(parent);

    node.ChildNodes.Add(child);

    node.SelectAction =
    TreeNodeSelectAction.Expand;

    }

    }

    get.Close();

    }

    protected TreeNode Find_Node(int parent)

    {

    TreeNode node = new TreeNode ();for (int i = 0; i < trvGroup.Nodes.Count; i++)

    {

    node = trvGroup.Nodes[i];

    if (string.Compare(node.Value, parent.ToString()) == 0)

    {

    break;

    }

    node = Find_Node1(node, parent);

    if (null != node)

    {

    break;

    }

    }

    return node;

    }

    protected TreeNode Find_Node1(TreeNode node, int parent)

    {

    TreeNode child = new TreeNode ();for (int i = 0; i < node.ChildNodes.Count; i++)

    {

    child = node.ChildNodes[i];

    if (string.Compare(child.Value, parent.ToString()) == 0)

    {

    break;

    }

    child = Find_Node1(child, parent);

    if (null != child)

    {

    break;

    }

    }

    return child;

    }

  • Re: How to use treeview-dynamica put nodes values?

    03-12-2009, 9:58 AM
    • Member
      8 point Member
    • bjcanada
    • Member since 06-17-2008, 5:12 PM
    • Posts 108

    bjcanada:

    I got another solution for that, not use the xml file. The code is below.

    trvGroup is name of treeview control  

    Hi All,

    That solution is not all correct, can only show correct in the left branch, the right branch not show.

    can someone help me to solve it. 

    Thanks in advance!

  • Re: How to use treeview-dynamica put nodes values?

    07-16-2009, 1:02 PM
    • Member
      8 point Member
    • bjcanada
    • Member since 06-17-2008, 5:12 PM
    • Posts 108

     The new solution a did like below. and include the save treeview's state.

        public partial class WUCConferenceGroup : BaseControl
        {
            BLCommons blc = new BLCommons();
    
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                   LoadTree(UIGCommons.CurrentClientID);
                   if (null == Session["TreeView"])
                   {
                       trvGroup.ExpandAll();
                       //StoreTreeViewStateToSession(trvGroup);
                   }
                   else 
                   {
                       RestoreTreeViewStateFromSession(trvGroup);
                   }
                   if (UIGCommons.CurrentConfID != -1)
                   {
                       GoThrough(trvGroup.Nodes[0]);
                   }
                }
            }
    
            protected void GoThrough(TreeNode fNode)
            {
                if (null != fNode.Parent)
                {
                    if ((string.Compare(fNode.Value, UIGCommons.CurrentConfID.ToString()) == 0) && (string.Compare(fNode.Parent.Value, UIGCommons.CurrentConfGroupID.ToString()) == 0))
                    {
                        fNode.Selected = true;
                    }
                }
                foreach (TreeNode child in fNode.ChildNodes)
                {
                    GoThrough(child);
                }
            }
    
            protected void LoadTree(int clientID)
            {
                TreeNode node = new TreeNode();
    
                string str1 = "Select isnull(max(ClientShortName),'') from Client where ClientID = '" + clientID + "' ";
                SqlDataReader na = blc.BLCommonsGetDataAllSTP(str1);
                while (na.Read())
                {
                    ViewState["ClientShotName"] = na.GetString(0) + " Groups";
                }
                na.Close();
    
                TreeNode root = new TreeNode();
                root.Text = (string)ViewState["ClientShotName"];
                root.Value = "0";
                trvGroup.Nodes.Clear();
                trvGroup.Nodes.Add(root);
    
                string str = "Select GroupID,Title From ConferenceGroup" +
                             " where ConferenceGroup.ClientID = '" + clientID + "' and ParentGroupID is null order by Title";
                SqlDataReader get = blc.BLCommonsGetDataAllSTP(str);
                while (get.Read())
                {
                    TreeNode child = new TreeNode();
                    child.Text = get.GetString(1);
                    child.Value = get.GetInt32(0).ToString();
    
                    trvGroup.Nodes[0].ChildNodes.Add(child);
    
                    AddChild(child, get.GetInt32(0));
                }
                get.Close();
    
                AddConfsNotInGroup(root);
    
                //trvGroup.ExpandAll();
    
                SwitchNode();
            }
    
            protected void AddChild(TreeNode parent, int pID)
            {
                BLCommons blc = new BLCommons();
                string str = "Select GroupID,Title From ConferenceGroup" +
                             " where ConferenceGroup.ClientID = '" + UIGCommons.CurrentClientID + "' and ParentGroupID='" + pID + "' order by Title";
                SqlDataReader get = blc.BLCommonsGetDataAllSTP(str);
                while (get.Read())
                {
                    TreeNode child = new TreeNode();
                    child.Text = get.GetString(1);
                    child.Value = get.GetInt32(0).ToString();
    
                    parent.ChildNodes.Add(child);
                    AddChild(child, get.GetInt32(0));
                }
                if (!get.HasRows)
                {
                    AddConfs(parent, pID);
                    return;
                }
                get.Close();
    
                AddConfs(parent, pID);
            }
    
            protected void AddConfs(TreeNode parent,int gourpID) 
            {
                string str1 = "Select Conference.ConfID,Name from Conference" +
                              " inner join ConferenceConferenceGroupLink on ConferenceConferenceGroupLink.ConfID = Conference.ConfID" +
                              " where GroupID = '" + gourpID + "' order by Name";
                SqlDataReader get1 = blc.BLCommonsGetDataAllSTP(str1);
                while (get1.Read())
                {
                    TreeNode child1 = new TreeNode();
                    child1.Text = get1.GetString(1);
                    child1.Value = get1.GetInt32(0).ToString();
                    parent.ChildNodes.Add(child1);
                    if (-1==UIGCommons.CurrentConfID || child1.Value.Equals(UIGCommons.CurrentConfID.ToString()))
                    {
                        child1.Select();
                        UIGCommons.CurrentConfID = int.Parse(child1.Value);
                    }
                }
                get1.Close();
            }
    
            protected void AddConfsNotInGroup(TreeNode parent)
            {
                string strc = "Select ConfID,Name from Conference" +
                              " Where ClientID = "+ UIGCommons.CurrentClientID +" " +
                              " AND NOT EXISTS(SELECT 1 FROM ConferenceConferenceGroupLink WHERE ConferenceConferenceGroupLink.ConfID = Conference.ConfID) order by Conference.Name";
                SqlDataReader getc = blc.BLCommonsGetDataAllSTP(strc);
                while (getc.Read())
                {
                    TreeNode childc = new TreeNode();
                    childc.Text = getc.GetString(1);
                    childc.Value = getc.GetInt32(0).ToString();
                    parent.ChildNodes.Add(childc);
                    if (-1 == UIGCommons.CurrentConfID || childc.Value.Equals(UIGCommons.CurrentConfID.ToString()))
                    {
                        childc.Select();
                        UIGCommons.CurrentConfID = int.Parse(childc.Value);
                    }
                }
                getc.Close();
            }
    
            public override void SwitchClient(int ClientID)
            {
                LoadTree(UIGCommons.CurrentClientID);
                HttpContext.Current.Session.Remove("TreeView");
                trvGroup.ExpandAll();
                StoreTreeViewStateToSession();
            }
    
            protected void trvGroup_SelectedNodeChanged(object sender, EventArgs e)
            {
                SwitchNode();
            }
    
            private void SwitchNode() 
            {
                if ((null != trvGroup.SelectedNode) && (trvGroup.SelectedNode.ChildNodes.Count == 0))
                {
                    UIGCommons.CurrentConfID = Int32.Parse(trvGroup.SelectedNode.Value.ToString());
                    UIGCommons.CurrentConfName = trvGroup.SelectedNode.Text;
                    if (null == trvGroup.SelectedNode.Parent)
                    {
                        UIGCommons.CurrentConfGroupID = -1;
                    }
                    else
                    {
                        UIGCommons.CurrentConfGroupID = Int32.Parse(trvGroup.SelectedNode.Parent.Value.ToString());
                    }
                }
                else
                {
                    UIGCommons.CurrentConfID = -1;
                    UIGCommons.CurrentConfName = "Workspace";
                    UIGCommons.CurrentConfGroupID = -1;
                }
    
                SoftConference soft = (HttpContext.Current.CurrentHandler as Page).Master as SoftConference;
                soft.PutWorkBar();
            }
    
            public void StoreTreeViewStateToSession()
            {
                TreeView tvIn = new TreeView();
                
                tvIn = trvGroup;
    
                string strList = "";
    
                StoreTreeViewStateToSession_Recurse(tvIn.Nodes[0], ref strList);
    
                if (null != tvIn.SelectedNode && null != tvIn.SelectedNode.ValuePath)
                {
                    strList = tvIn.SelectedNode.ValuePath + strList;
                }
                Session["TreeView"] = strList;
            }
    
            private static void StoreTreeViewStateToSession_Recurse(TreeNode tnIn, ref string strList)
            {
                if (tnIn.Expanded == true)
                {
                    strList = "," + tnIn.ValuePath + strList;
                }
                foreach (TreeNode tnCurrent in tnIn.ChildNodes)
                {
                    StoreTreeViewStateToSession_Recurse(tnCurrent, ref strList);
                }
            }
    
            protected void RestoreTreeViewStateFromSession(TreeView tvIn)
            {
                if (Session["TreeView"] + "" != "")
                {
                    string strSelectedNodeIndex = "";
                    foreach (string strCurrent in Session["TreeView"].ToString().Split(','))
                    {
                        if (strSelectedNodeIndex == "")
                        {
                            strSelectedNodeIndex = strCurrent;
                        }
                        else
                        {
                            try
                            {
                                tvIn.FindNode(strCurrent).Expanded = true;
                            }
                            catch
                            {
                            }
                        }
                    }
    
                    try
                    {
                        TreeNode tnTest = tvIn.FindNode(strSelectedNodeIndex);
                        tvIn.FindNode(strSelectedNodeIndex).Select();
                        ((TreeNode)tvIn.FindNode(tvIn.SelectedNode.ValuePath).Parent).Expanded = true;
                    }
                    catch
                    {
                    }
    
                    //HttpContext.Current.Session.Remove("TreeView");
                }
            }
    
        }//end of class
    }
     
    below is where call the save treeview state
                if (null != Session["MenuFirstID"] && string.Compare((string)Session["MenuFirstID"], "3") == 0)
                {
                    WUCConferenceGroup cg = (WUCConferenceGroup)pnlShow.FindControl("ConfsGroup");
                    if (null != cg)
                    {
                        cg.StoreTreeViewStateToSession();
                    }
                }


     

Page 1 of 1 (6 items)