private void loadPower(TreeNode node,string PowerParentId)
{
DataRow[] dr = dsPower.Tables[0].Select("ParentId='"+PowerParentId+"'","ChildrenId");
foreach(DataRow idr in dr)
{
TreeNode tmp= node.Nodes.Add(idr["ChildrenId"].ToString(), idr["ChildrenName"].ToString());
DataRow[] drc = dsPower.Tables[0].Select("ParentId='" + idr["ChildrenId"].ToString() + "'", "ChildrenId");
if (drc.Length > 0)
loadPower(tmp, idr["ChildrenId"].ToString());
}
}
private void UserCodeEdit_Load(object sender, EventArgs e)
{
dsPower = dbManage.getDataSet("select * from Powers");
loadPower(treeView1.TopNode,"Super");
if (mstate == Public.mState.msEdit)
{
DataSet ds = dbManage.getDataSet("select * from Users where UserId ='" + _UserId + "'");
txtUserName.DataBindings.Add("Text", ds.Tables[0], "UserName");
txtUserId.DataBindings.Add("Text", ds.Tables[0], "UserId");
txtPass1.DataBindings.Add("Text", ds.Tables[0], "Password");
txtPass2.DataBindings.Add("Text", ds.Tables[0], "Password");
this.txtUserId.ReadOnly = true;
txtUserName.Focus();
}
}
以上是我写的一个权限控制的显示,其中中Powers表的结构为三个字段ParentId,ChildrenId和ChildrenName。其中根节点值为“Super”,这个表中的记录内容如下:
BoxManage BoxOrder 纸箱订单
BoxManage BoxQuo 纸箱报价
BoxOrder Modify 纸箱订单修改
Super BoxManage 纸箱管理
Super UserManage 用户管理
使用了递归,Perfect!
结果如图: