递归绑定

private void DropDownListBind()
    {
        ddl.Items.Clear();
        dt = Productmanage.Instance.GetAllProCate();
        if (dt != null)
            bindTree(dt, 0, 0);        
        ddl.Items.Insert(0, new ListItem("所有分类", "0"));
    }

    private void bindTree(DataTable dt, int parentId, int dep)
    {
        dep++;
        foreach (DataRow dr in dt.Select("parent_cid=" + parentId))
        {
            ddl.Items.Add(new ListItem(showNull(dep) + dr["name"], dr[0].ToString()));
            bindTree(dt, Convert.ToInt32(dr["cid"]), dep);
        }
    }

    private string showNull(int dep)
    {
        string str = null;
        for (int i = 1; i < dep; i++)
            str += " ";
        return str + "|-";
    }

 

posted @ 2012-11-13 17:40  一千零一夜  阅读(139)  评论(0编辑  收藏  举报