部门树学习笔记-为了忘却的纪念

 一年前学习了部门树,因为当时研究完没有做笔记,现在已经忘得差不多了,只知道用到了递归算法。现在回想起来,之所以忘记,是因为理解的不够透彻。今天研究了一天,已经大彻大悟。

 部门树的数据表(deptinfo)一共有三个字段,分别为deptid,pid,deptname,这三个字段的意思我不说大家也明白。

部门树的思想:

第一步,查询出所以部门信息。

public DataTable GetAllDeptInfo()
{
           string connstr = "data source=localhost;Integrated Security=SSPI;initial catalog=Study";//数据库连接字符串有多种,因为我的数据库采用的是Windows认证方式,所以采用Integrated Security=SSPI,若不是则采用uid=用户名;pwd=密码。
            SqlConnection conn=    new SqlConnection(connstr);
            if(conn.State==ConnectionState.Closed)
            conn.Open();
            SqlCommand com = conn.CreateCommand();
            string sql = "select * from DeptInfo";
            DataTable myTable = new DataTable();
            com.CommandText=sql;
            SqlDataAdapter sda = new SqlDataAdapter(com);
            sda.Fill(myTable);   
            if (conn.State == ConnectionState.Open) conn.Close();
            return myTable;

}

protected void Page_Load(object sender, EventArgs e)
{
            DataTable dtParam = GetAllDeptInfo();
            DataTable dtTree = dtParam.Clone();

            //克隆出与dtParam一样架构但没有数据的表
            BuildTree(dtTree, dtParam, 0, "0");//从根部门出发
            DropDownList1.DataSource = dtTree;
            DropDownList1.DataTextField = dtTree.Columns[2].ToString();
            DropDownList1.DataValueField = dtTree.Columns[0].ToString();
            DropDownList1.DataBind();
 }

接下来写递归方法,思想为dtTree加入当前的行,并将非当行前指派到下一递归。

public void BuildTree(DataTable dtTree, DataTable dtParam, int intLevel, string parentid)
{
            intLevel++;
            string strLeftPre = "";

  //设置前辍
       if (intLevel > 1)
            {
                strLeftPre = "|" + strLeftPre.PadLeft(intLevel*2,'-');
            }
            DataTable currTable = new DataTable();
            currTable = dtParam.Clone();//用来记录当前行
         DataTable nextTable = new DataTable();
            nextTable = dtParam.Clone();//用来记录非当前行,也就是剩余行,此方法很好,不需要重复遍历。
      for (int i = 0; i < dtParam.Rows.Count; i++)
            {
                if (dtParam.Rows[i][1].ToString() == parentid)//判断是否为当前行,如是加入currTable,否则加入nextTable

           currTable.Rows.Add(dtParam.Rows[i].ItemArray);
                }
                else
                {
                    nextTable.Rows.Add(dtParam.Rows[i].ItemArray);
                }
            }
            for (int j = 0; j < currTable.Rows.Count; j++)
            {
                DataRow dr = currTable.Rows[j];
                dr[2] = strLeftPre + dr[2].ToString();
                dtTree.Rows.Add(dr.ItemArray);//将当前行插入dtTree
      BuildTree(dtTree, nextTable, intLevel, dr[0].ToString());//处理当前行的子部门
       }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值