首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

DropDownList中绑定树形结构,在线求解答.解决方案

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

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);
conn.Close();
}



/// <summary>
/// 递归绑定树节点
/// </summary>
/// <param name="parentId">父节点编号</param>
/// <param name="parentnode">父节点</param>
protected void addTree(string parentId, TreeNode parentnode)
{
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
if (parentId == ds.Tables[0].Rows[i][2].ToString().Trim() || parentId == null)
{
TreeNode node = new TreeNode(ds.Tables[0].Rows[i][1].ToString().Trim(),
ds.Tables[0].Rows[i][0].ToString().Trim(), string.Empty,
string.Format("DeptInfoMain.aspx?deptID={0}", ds.Tables[0].Rows[i][0].ToString().Trim()),
string.Format("DeptInfo"));

addTree(ds.Tables[0].Rows[i][0].ToString().Trim(), node);
if (parentnode == null)
{
trvDeptInfo.Nodes.Add(node);
break;
}
else
{
parentnode.ChildNodes.Add(node);
}
}
}
}
[解决办法]
DECLARE @t_Level TABLE(ID char(3),Level int,Sort varchar(8000))
DECLARE @Level int
SET @Level=0
INSERT @t_Level SELECT ID,@Level,ID
FROM sssss
WHERE PID = 0
WHILE @@ROWCOUNT>0
BEGIN
SET @Level=@Level+1
INSERT @t_Level SELECT a.ID,@Level, b.ID
FROM sssss a,@t_Level b
WHERE a.PID=b.ID
AND b.Level=@Level-1
END
 
SELECT a.id,a.pid, SPACE(b.Level*2)+' |--'+a.Name,b.sort
FROM sssss a,@t_Level b
WHERE a.ID=b.ID
ORDER BY b.Sort,a.id
[解决办法]
public class DDLDepartment : DropDownList
{
public DDLDepartment()
{
bind(this, 0);
this.Items.Insert(0, new ListItem("==请选择==", ""));
}
public void bind(DropDownList ddlDepartment, int parent)
{
IList<Department> deptlist = DepartmentBLL.SelectChild(parent);
foreach (Department dept in deptlist)
{
string text = new string(' ', dept.Depth - 1);
text += "└" + dept.DeptName;
ddlDepartment.Items.Add(new ListItem(text, dept.DeptId.ToString()));
bind(this, dept.DeptNo);
}

}
public bool Blank
{
set
{
if (value == false) this.Items.RemoveAt(0);
}

}
}

热点排行