数据库表图
要显示的结果图 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())); }
}
要显示的结果图 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())); }
}
DeptID DeptName ParentDept
18 舞蹈部 0
19 球类部 0
20 武术部 0
21 民族舞蹈 18
22 交易舞蹈 18
23 街舞组 18
24 南拳组 20
25 北腿组 20
/// 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);
}
}
}
你先在是形式上实现树形,自己定制个控件用treeview实现,将数据源作为参数传进去。
这是我原来写的一个,其中只允许有一个顶级结点(如果你有多个并列的顶级结点,不妨把他们归入一个顶级结点下面),顶级结点的父节点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);
}
}
}
}
调用
BindDropDownList2(this.DropDownList1, dt, "0", "DeptID", "ParentDept", "DeptName", -1, "");
总共用哪个集合 集合1 集合2 集合3
把一个父节点放到一个集合2
然后循环父节点 先把父节点添加到集合1中 然后找出该节点下的子节点 集合3
把集合3都放入集合1
这样集合1就是你想要得到的数据了
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 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);
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;
using System.Web;
using System.Web.UI;
using System.ComponentModel;
using System.Data.SqlClient;
using System.Data;namespace chkipsrv
{
[DefaultProperty("Text"), ToolboxData("<{0}:Dropaaa runat=server></{0}:Dropaaa>")]
public class Dropaaa : System.Web.UI.WebControls.DropDownList, INamingContainer
{
public static string connction = "server=.;database=stu;Integrated Security=True";
private string toadd = "├";
private int _DepartID; public Dropaaa()
{ } public int DepartID
{
get { return _DepartID; }
set {
_DepartID = value;
GetArticleCategory(_DepartID,12);
}
}
private void GetArticleCategory(int deptid,int depttype)
{
SqlConnection conn = new SqlConnection(connction);
string sql = "select * from UserDept where deptid=" + deptid + "and depttype=" + depttype + "";
SqlCommand cmd = new SqlCommand(sql, conn);
conn.Open();
SqlDataReader sdr = cmd.ExecuteReader();
while (sdr.Read())
{ this.Items.Add(new ListItem(toadd + " " + sdr["deptname"].ToString(), sdr[1].ToString()));
toadd += "─┴";
this.GetArticleCategory( Convert.ToInt32( sdr[0].ToString()),depttype);
toadd = toadd.Substring(0, toadd.Length - 2); //开始这里没有处理好,层次关系显示不出来 :)
}
sdr.Close();
conn.Close();
}
}
}