树视图上的循环问题

发布于 2024-09-25 09:46:57 字数 4672 浏览 0 评论 0原文

这可能非常简单,但我遇到了一个非常奇怪的问题,我编写的用于将菜单项填充到树视图的循环不显示子节点 ID。它提取的数据在 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

当尝试填充树视图时,它将树中选择节点的parentOptionID输出到标签 - 问题是它没有选择子节点的parentOptionID,在本例中是测试、测试2和测试。这是我正在使用的代码..

 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.
        }

谢谢

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. The data it is pulling is in the following format on the 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. 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

凉城 2024-10-02 09:46:57

解决了。我需要返回nodeList;在其他之后。

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; 
        } 

Worked it out. I needed return 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; 
        } 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文