locked
How to not showing root node if there is no child nodes in treeview RRS feed

  • Question

  • User2144323856 posted

    I'm currently binding my treeview from my database. Now by using the code below, if there is data in DocModule, it will be the child nodes of the SubModule. But not every SubModule has the data of DocModule, so means some of the root node will not having child nodes, how can i not showing the root node if there is no child nodes?

    if (!Page.IsPostBack)
                {
                    string connection = ConfigurationManager.ConnectionStrings["dbITAsset"].ConnectionString;
                    using (SqlConnection Conn = new SqlConnection(connection))
                    {
                        string SubModule = "Select * from SUBMODULE";
                        string DocModule = "Select * from MENUTRANSACTION WHERE UID = '" + Session["UserID"] + "' ";
                        string Treeview = SubModule + ";" + DocModule;

                        DataSet ds = new DataSet();
                        SqlDataAdapter da = new SqlDataAdapter(Treeview, Conn);
                        da.Fill(ds);
                        ds.Tables[0].TableName = "SUBMODULE";
                        ds.Tables[1].TableName = "MENUTRANSACTION";

                        DataRelation dr = new DataRelation("SUBDOCMODULE", ds.Tables["SUBMODULE"].Columns["SUBMODULEID"], ds.Tables["MENUTRANSACTION"].Columns["SUBMODULEID"]);
                        ds.Relations.Add(dr);

                        foreach (DataRow drState in ds.Tables["SUBMODULE"].Rows)
                        {
                            TreeNode NDState = new TreeNode();
                            NDState.Text = drState["SUBMODULENM"].ToString();
                            NDState.Value = drState["SUBMODULEID"].ToString();
                            NDState.NavigateUrl = drState["URLNAME"].ToString();
                            TreeView2.Nodes.Add(NDState);

                            foreach (DataRow drCity in drState.GetChildRows("SUBDOCMODULE"))
                            {
                                TreeNode NDCity = new TreeNode();
                                NDCity.Text = drCity["DNAME"].ToString();
                                NDCity.Value = drCity["DID"].ToString();
                                NDCity.NavigateUrl = drCity["DADDRESS"].ToString();
                                NDState.ChildNodes.Add(NDCity);
                            }
                        }
                    }
                } 

    Tuesday, March 30, 2021 1:34 AM

Answers

  • User-177150550 posted

    Try this 

     foreach (DataRow drState in ds.Tables["SUBMODULE"].Rows)
                        {
                            TreeNode NDState = new TreeNode();
                            NDState.Text = drState["SUBMODULENM"].ToString();
                            NDState.Value = drState["SUBMODULEID"].ToString();
                            NDState.NavigateUrl = drState["URLNAME"].ToString();
                            TreeView2.Nodes.Add(NDState);            

               foreach (DataRow drCity in drState.GetChildRows("SUBDOCMODULE"))
                            {
                                TreeNode NDCity = new TreeNode();
                                NDCity.Text = drCity["DNAME"].ToString();
                                NDCity.Value = drCity["DID"].ToString();
                                NDCity.NavigateUrl = drCity["DADDRESS"].ToString();
                                NDState.ChildNodes.Add(NDCity);
                            } if ( NDState.ChildNodes.Count == 0 ){

    TreeView2.Nodes.Remove(NDState);

    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, March 30, 2021 1:57 AM