我明白咯
我刚刚做过了
给你代码自己去看啊!!
void TreeBind()
{
TreeView1.Nodes.Clear();
dv = Client.ClienteleByUID(int.Parse(Session["U_id"].ToString())) as DataView;
foreach (DataRowView dvrow in dv)
{
if (dvrow["CLCode"].ToString().Trim().Length == 2)
{
TreeNode root = new TreeNode();
root.Text = dvrow["CLName"].ToString();
root.Value = dvrow["CLid"].ToString();
root.Target = dvrow["CLCode"].ToString().Trim();
TreeView1.Nodes.Add(root);
CreateChildNode(root);
}
TreeView1.ExpandAll();
}
}
void CreateChildNode(TreeNode parentNode)
{
foreach (DataRowView i in dv)
{
//遍历子节点的id长度0001=父节点的00+2子节点的Id截取0001(0,父节点长度)==00,比较子节点Id的前两位是否=父节点Id
if (i["CLCode"].ToString().Trim().Length == parentNode.Target.Length + 2 && i["CLCode"].ToString().Trim().Substring(0, parentNode.Target.Length) == parentNode.Target)
{
TreeNode child = new TreeNode();
child.Text = i["CLName"].ToString();
child.Value = i["CLid"].ToString();
child.Target = i["CLCode"].ToString().Trim();
parentNode.ChildNodes.Add(child);
CreateChildNode(child);
}
TreeView1.ExpandAll();
}
}
这是按扭事件里的:
int Su_id = int.Parse(ddlClientName.SelectedItem.Value);
int U_id = int.Parse(Session["U_id"].ToString());
string ProductsName = ddlName.SelectedItem.Text;
string CLName = txtCLName.Text;
string CLCode = "";
DataView dv = Client.ClienteleByUID(U_id) as DataView;
if (dv.Count > 0)
{
if (string.IsNullOrEmpty(TreeView1.SelectedValue))
{
("请选择节点!");
}
else
{
string Code = TreeView1.SelectedNode.Target;//0
//查询code所在的节点下有多少子节点再依次递增
DataSet ds = Client.ClienteleByName(Code);
int size = int.Parse(ds.Tables[0].Rows[0]["id"].ToString());//2
int js = size + 1;//2+1=3
if (size > 9)
CLCode = Code + js.ToString();//0001+11
else
CLCode = Code + "0" + js.ToString();//0001+0+3
}
}
else
{
CLCode = "00";
}
if (Client.ClienteleAdd(CLName, CLCode, ProductsName, U_id, Su_id))
{
("添加成功!");
}
else
{
("添加失败!");
}
TreeBind();