c# treeview控件动态绑定数据库,实现2级目录,当输入同一父节点的子节点时,在相同父节点下添加子节点.txt

 private void LoadData()
        {
            try
            {          
                //得到所有所有父节点,放到DataTable中,这里默认根节点的父节为0
                BindRootNode(InitRootNodeDataTable(), treeCatalog.Nodes);   //绑定所有的父节点
            }
            catch (Exception ex)
            {
                UDS.Components.Error.Log(ex.ToString());
            }
        }

        #region 得到父节点的字节点,放到DataTable中
        ///


        /// 初始化 RootNode DataTable
        ///

        private DataTable InitRootNodeDataTable()
        {
            Database data = new Database();
            SqlDataReader dataReader = null;

            try
            {
                String classSQL = "SELECT *,1 as childNum FROM UDS_Class WHERE ClassID = " + topID.ToString();
                dataReader = new Database().ExSQLReDr(classSQL);
            }
            catch (Exception ex)
            {
                UDS.Components.Error.Log(ex.ToString());
            }
            DataTable t = Tools.ConvertDataReaderToDataTable(dataReader);
            dataReader.Close();
            t.TableName = "TreeView";
            return t;
        }

        ///


        /// 初始化 ChildNode DataTable
        ///

        private DataTable GetChildNodeDataTable(int ClassParentID)
        {
            Database data = new Database();
            SqlDataReader dataReader = null;
            string sql = "select *, (select count(*) from uds_class where Display < 1 and classParentID = a.ClassID and classParentid <> ClassID) as childNum from uds_class a where Display < 1 and classParentid =" + ClassParentID + " and classParentid <> ClassID order by DocSort";
            try
            {
                dataReader = data.ExSQLReDr(sql); ;
            }
            catch (Exception ex)
            {
                Response.Write(ex.ToString());
                UDS.Components.Error.Log(ex.ToString());
            }
            DataTable t = Tools.ConvertDataReaderToDataTable(dataReader);
            dataReader.Close();
            t.TableName = "TreeView";
            return t;
        }
        #endregion

        #region 填充节点
        private void BindRootNode(DataTable table, TreeNodeCollection node)
        {
            DataView dv = new DataView(table);
            foreach (DataRowView drv in dv)
            {
                TreeNode NewNode = new TreeNode();
                NewNode.Value = drv["ClassID"].ToString();
                NewNode.Text = "" + drv["ClassName"].ToString() + "";
                NewNode.ImageUrl = GetIcon(drv["ClassType"].ToString());
                NewNode.NavigateUrl = "Default.aspx?topID=" + NewNode.Value;
                NewNode.Target = "_top";
                node.Add(NewNode);

                //判断是否还有子节点,如果还有子节点,显示展开符号
                if (Convert.ToInt32(drv["childNum"]) > 0)
                    NewNode.PopulateOnDemand = true;
            }
        }

        private void BindNode(DataTable table, TreeNodeCollection node)
        {
            DataView dv = new DataView(table);
            foreach (DataRowView drv in dv)
            {
                TreeNode NewNode = new TreeNode();
                NewNode.Value = drv["ClassID"].ToString();
                NewNode.Text = "" + drv["ClassName"].ToString() + "";
                switch (drv["ClassType"].ToString())
                {
                    case "0":
                        NewNode.NavigateUrl = "Default.aspx?topID=" + NewNode.Value;
                        NewNode.Target = "_top";
                        break;
                    case "1":
                        NewNode.NavigateUrl = "Document/listview.aspx?topID=" + topID.ToString() + "&ClassID=" + NewNode.Value;
                        NewNode.Target = "MainFrame";
                        break;
                }
                node.Add(NewNode);

                //判断是否还有子节点,如果还有子节点,显示展开符号
                if (Convert.ToInt32(drv["childNum"]) > 0)
                    NewNode.PopulateOnDemand = true;
            }
        }

        #region 获取节点图标
        ///


        /// 获取节点图标
        ///

        private string GetIcon(string ClassType)
        {
            string rtnValue = "DataImages/";
            switch (ClassType)
            {
                case "0":
                    rtnValue += "flag.gif";
                    break;
                case "1":
                    rtnValue += "myDoc.gif";
                    break;
                case "2":
                    rtnValue += "mail.gif";
                    break;
                case "3":
                    rtnValue += "page.gif";
                    break;
                case "4":
                    rtnValue += "staff.gif";
                    break;
                case "5":
                    rtnValue += "help_page.gif";
                    break;
                case "6":
                    rtnValue += "MyTask.gif";
                    break;
                case "7":
                    rtnValue += "mail.gif";
                    break;
                case "8":
                    rtnValue += "myDoc.gif";
                    break;
                case "9":
                    rtnValue += "DocFlow.gif";
                    break;
                case "10":
                    rtnValue += "ClientManage.gif";
                    break;
                case "11":
                    rtnValue += "myLinkman.gif";
                    break;
                case "12":
                    rtnValue += "position.gif";
                    break;
                case "13":
                    rtnValue += "roleManage.gif";
                    break;
                case "14":
                    rtnValue += "kaoqin.gif";
                    break;
                case "15":
                    rtnValue += "workadmin.gif";
                    break;
                case "16":
                    rtnValue += "message.gif";
                    break;
                case "17":
                    rtnValue += "usbkey.gif";
                    break;
                default:
                    rtnValue += "red_ball.gif";
                    break;
            }
            return rtnValue;
        }
        #endregion

        #endregion

        #region 填充节点事件
        protected void TreeCatalog_TreeNodePopulate(object sender, TreeNodeEventArgs e)
        {
            getDataNode(e.Node.Value, e.Node);     //点加号展开时调用.得到数据并绑定.传入点击结点 ID,和点击节点对像.
        }
        #endregion

        private void getDataNode(String ParentId, TreeNode Node)
        {
            BindNode(GetChildNodeDataTable(Convert.ToInt16(ParentId)), Node.ChildNodes);      //向结点填充数据.
        }

你可能感兴趣的:(处世绝学)