简体   繁体   English

树视图上的循环问题

[英]Loop issue on treeview

This maybe something really simple but I got a really strange issue where a loop i have written to populate menu items to a treeview is not displaying childnode id. 这也许很简单,但是我遇到了一个非常奇怪的问题,我编写的将菜单项填充到树形视图的循环未显示childnode id。 The data it is pulling is in the following format on the sql. 它要提取的数据在sql上采用以下格式。

OptionID       OptionText    displayOrder    parentOptionID     optionDeleted
        226     test menu     0               0                  FALSE
        227     test menu1    0               226                FALSE
        228     test menu2    0               227                FALSE
        229     test menu 3   0               228                FALSE
        230     test          0               229                FALSE
        231     test 2        3               228                FALSE
        232     test 3        6               229                FALSE

When try the treeview is populated it outputs the parentOptionID of the select node in the tree to a label - the problem is it does not pick the the parentOptionID of the childs, in this case test, test 2 and test. 尝试填充树视图时,它会将树中选择节点的parentOptionID输出到标签-问题是它没有选择子级的parentOptionID,在这种情况下为test,test 2和test。 This is the code i am using.. 这是我正在使用的代码。

 private void populateRootLevelAll(bool ireset)
        {
            // Locals

            Functionality func = new Functionality();
            SqlConnection supportDB = null;
            DataTable t = new DataTable();
            bool opDeleted = false;

            string spName = "gssp_TechCallTrackerOptionsSelectAllWithDelete";

            try
            {
                using (supportDB = new SqlConnection(getConnectionString(ConnectionType.GriffinSupportDB)))
                {
                    using (SqlCommand getCallTrackerOptions = new SqlCommand(spName, supportDB))
                    {
                        // Now set up the rest of the command object
                        getCallTrackerOptions.CommandType = CommandType.StoredProcedure;


                        // Populate the parameters.
                        getCallTrackerOptions.Parameters.Clear();
                        getCallTrackerOptions.Parameters.Add(func.CreateParameter("@optionDeleted", SqlDbType.Bit, ParameterDirection.Input, opDeleted));
                        getCallTrackerOptions.Parameters.Add(func.CreateParameter("@spErrorID", SqlDbType.Int, ParameterDirection.Output, DBNull.Value));

                        // Set up the dataset
                        supportDB.Open();
                        SqlDataAdapter da = new SqlDataAdapter(getCallTrackerOptions);
                        da.Fill(t);
                        supportDB.Close();

                        // Build TreeList Nodes.
                        foreach (DataRow r in t.Rows)
                        {
                            // add top level rows and then add their child rows on each itteration.
                            if (r["parentOptionID"].ToString() == "0")
                            {
                                TreeNode masterNode = new TreeNode(r["optionText"].ToString());
                                if (GetChildNodes(r["OptionID"].ToString(), t) != null)
                                {

                                    List<TreeNode> childNodeList = GetChildNodes(r["OptionID"].ToString(), t);
                                    masterNode.Nodes.AddRange(childNodeList.ToArray());

                                }


                                masterNode.Tag = "0";
                                masterNode.Name = r["OptionID"].ToString();
                                TVCTOptions.Nodes.Add(masterNode);

                            }
                        }

                    }
                }
            }

            catch (Exception e)
            {
                throw e;
            }
        }

        static List<TreeNode> GetChildNodes(string parentOptionID, DataTable t)
        {

            List<TreeNode> nodeList = new List<TreeNode>();
            foreach (DataRow r in t.Rows)
            {
                if (r["parentOptionID"].ToString() == parentOptionID)
                {
                    // create child
                    TreeNode node = new TreeNode(r["optionText"].ToString());
                    node.Tag = parentOptionID.ToString();

                    // check if this child has children.
                    if (GetChildNodes(r["OptionID"].ToString(), t) != null)
                    {
                        node.Nodes.AddRange(GetChildNodes(r["OptionID"].ToString(), t).ToArray());
                        node.Name = (r["OptionID"].ToString());

                    }
                    else
                    {
                        node.Tag = parentOptionID.ToString();
                    }

                    nodeList.Add(node);
                }
            }

            if (nodeList.Count != 0)
                return nodeList;

            else
                return null;    // returns null when no children were found.
        }

Thanks 谢谢

Worked it out. 解决了。 I needed return nodeList; 我需要返回nodeList; after the else. 在其他之后。

static List<TreeNode> GetChildNodes(string parentOptionID, DataTable t) 
        { 

            List<TreeNode> nodeList = new List<TreeNode>(); 
            foreach (DataRow r in t.Rows) 
            { 
                if (r["parentOptionID"].ToString() == parentOptionID) 
                { 
                    // create child 
                    TreeNode node = new TreeNode(r["optionText"].ToString()); 
                    node.Tag = parentOptionID.ToString(); 

                    // check if this child has children. 
                    if (GetChildNodes(r["OptionID"].ToString(), t) != null) 
                    { 
                        node.Nodes.AddRange(GetChildNodes(r["OptionID"].ToString(), t).ToArray()); 
                        node.Name = (r["OptionID"].ToString()); 

                    } 
                    else 
                    { 
                        node.Tag = parentOptionID.ToString(); 
                    } 

                    nodeList.Add(node); 
                } 
            } 

            if (nodeList.Count != 0) 
                return nodeList; 

            else 
                return nodeList; 
        } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM