递归绑定

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 + "|-";
    }
原文地址:https://www.cnblogs.com/daixingqing/p/2768417.html