日期:2014-05-18  浏览次数:20768 次

DropDownList中绑定树形结构,在线求解答....
数据库表图
要显示的结果图
C# code

 private void InitDepartDropDown()
        {

            CustomerControlInfo info;
            this.Items.Clear();
            this.Items.Add(new ListItem("请选择成员", "0"));
            int OwnerUserId = GetOwnerUserID();
            ArrayList al = CustomerControlController.GetUCPBerInfoDeptName(OwnerUserId);
            if (al.Count > 0)
            {
                for (int i = 0; i < al.Count; i++)
                {
                     info = (CustomerControlInfo)al[i];
                    int UserId = info.UserId;
                    int ParentDept = info.ParentDept;
                    string DeptName = info.DeptName;
                    if (ParentDept>0)
                        {
                            DeptName = "**|-" + DeptName;
                        }
                        else
                        {
                            DeptName = "|--" + DeptName;
                        }
                    this.Items.Add(new ListItem(DeptName, UserId.ToString()));

                }
            }




------解决方案--------------------
C# code
 /// <summary>
        /// lcl版把一个无限循环数据表绑定数据到一个DropDownList下拉列中,并分层显示
        /// </summary>
        /// <param name="DropDownList">绑定的控件</param>
        /// <param name="dt">需要的dt</param>
        /// <param name="firstfather_id">第一项的父ID值</param>
        /// <param name="childid">子ID字段名</param>
        /// <param name="father_id">父ID字段名</param>
        /// <param name="name">显示字段名</param>
        /// <param name="needlayer">显示的层数,为负值则不限</param>
        /// <param name="firstnull">为定值""</param>
        public static void BindDropDownList2(System.Web.UI.WebControls.DropDownList DropDownList, System.Data.DataTable dt, string firstfather_id, string childid, string father_id, string name, int needlayer, string firstnull)
        {
            if (needlayer > 0 || needlayer < 0)
            {
                System.Data.DataRow[] drs = dt.Select(father_id + "=" + firstfather_id);
                for (int i = 0; i < drs.Length; i++)
                {
                    DropDownList.Items.Add(new System.Web.UI.WebControls.ListItem(firstnull + drs[i][name].ToString(), drs[i][childid].ToString()));
                    string firstnull1 = "";
                    if (firstnull.IndexOf("|--") >= 0)
                    {
                        firstnull1 = "  " + firstnull;
                    }
                    else
                    {
                        firstnull1 = " |--";
                    }
                    BindDropDownList2(DropDownList, dt, drs[i][childid].ToString(), childid, father_id, name, needlayer - 1, firstnull1);
                }
            }
        }

------解决方案--------------------
[code=C#][/code]
这是我原来写的一个,其中只允许有一个顶级结点(如果你有多个并列的顶级结点,不妨把他们归入一个顶级结点下面),顶级结点的父节点id默认为空
/// <summary>
/// 初始化树接点
/// </summary>
protected void initTreeNode()
{
conn = new OracleConnection(OraDataConfig.getConnString());
conn.Open();

OracleDataAdapter adapter = new OracleDataAdapter("select DEPARTMENT_ID,department_code,PARENT_DEPT_ID from t_department order by DEPARTMENT_ID asc ", conn);
ds = new DataSet();
adapter.Fill(ds);
adapter.Dispose();
addTree(null, null);