TreeView绑定无限层级关系类

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Bind_TV(TreeView1.Nodes);
}
}
#region 添加父节点
///
/// 添加父节点
///
///
private void Bind_TV(TreeNodeCollection tree)
{
//先添加第一级父节点
uint id = Convert.ToUInt32(Session["ID"]);//登陆人id
string sqlmcount = "select * from account where status=1 and id='" + id + "'";
DataTable Trdtmcount = MySqlDbHelper.GetDataTable(sqlmcount, null);
if (Trdtmcount.Rows.Count > 0)
{
TreeNode tn = new TreeNode();
tn.Value = Trdtmcount.Rows[0]["id"].ToString();
tn.Text = Trdtmcount.Rows[0]["nickname"].ToString();
tree.Add(tn);
BindSon(Convert.ToUInt32(Trdtmcount.Rows[0]["id"]), tn.ChildNodes);//根据父节点添加子节点
}
}
#endregion
 
#region 递归添加子节点
///
/// 递归添加子节点
///
///
///
private void BindSon(uint fatherid, TreeNodeCollection tree)
{
string sqlMancount = "select id,nickname from account where masterid=" + fatherid + ";";
DataTable sonDT = MySqlDbHelper.GetDataTable(sqlMancount, null);
 
DataView dv = new DataView(sonDT);
foreach (DataRowView item in dv)
{
//判断此人id 是否有下属
if (sonDT.Rows.Count > 0)//有
{
TreeNode sontn = new TreeNode();
sontn.Value = item["id"].ToString();
sontn.Text = item["nickname"].ToString();
tree.Add(sontn);
BindSon(Convert.ToUInt32(item["id"]), sontn.ChildNodes);
}
}
}
#endregion

转载于:https://www.cnblogs.com/hww9011/p/3669193.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值