根据特殊的表数据,生成树
用的是最笨的方法,哪位有好的方法可以指教一下
Code
protected void btGetTreeview_Click(object sender, EventArgs e)
{
TreeView1.Nodes.Clear();
TreeView1.Visible = true;
DataSet ds = pkhead.readData("select * from [group] where group_code='00'");
if (ds.Tables.Count == 0) return;
foreach (DataRow dr in ds.Tables[0].Rows)
{
TreeNode treenode = new TreeNode();
treenode.Text = pubfunc.ObjectToString(dr["group_name"]);
treenode.Value = pubfunc.ObjectToString(dr["group_code"]);
TreeView1.Nodes.Add(treenode);
string sql2 = string.Format("select * from [group] where group_code like '{0}%' and len=2 order by group_code", treenode.Value);
DataSet ds2 = pkhead.readData(sql2);
if (ds2.Tables.Count == 0) return;
foreach (DataRow dr2 in ds2.Tables[0].Rows)
{
TreeNode treenode2 = new TreeNode();
treenode2.Text = pubfunc.ObjectToString(dr2["group_name"]);
treenode2.Value = pubfunc.ObjectToString(dr2["group_code"]);
treenode.ChildNodes.Add(treenode2);
string sql3 = string.Format("select * from [group] where group_code like '{0}%' and len=3 order by group_code", treenode2.Value);
DataSet ds3 = pkhead.readData(sql3);
if (ds3.Tables.Count == 0) return;
foreach (DataRow dr3 in ds3.Tables[0].Rows)
{
TreeNode treenode3 = new TreeNode();
treenode3.Text = pubfunc.ObjectToString(dr3["group_name"]);
treenode3.Value = pubfunc.ObjectToString(dr3["group_code"]);
treenode2.ChildNodes.Add(treenode3);
string sql4 = string.Format("select * from [group] where group_code like '{0}%' and len=4 order by group_code", treenode3.Value);
DataSet ds4 = pkhead.readData(sql4);
if (ds4.Tables.Count == 0) return;
foreach (DataRow dr4 in ds4.Tables[0].Rows)
{
TreeNode treenode4 = new TreeNode();
treenode4.Text = pubfunc.ObjectToString(dr4["group_name"]);
treenode4.Value = pubfunc.ObjectToString(dr4["group_code"]);
treenode3.ChildNodes.Add(treenode4);
string sql5 = string.Format("select * from [group] where group_code like '{0}%' and len=5 order by group_code", treenode4.Value);
DataSet ds5 = pkhead.readData(sql5);
if (ds4.Tables.Count == 0) return;
foreach (DataRow dr5 in ds5.Tables[0].Rows)
{
TreeNode treenode5 = new TreeNode();
treenode5.Text = pubfunc.ObjectToString(dr5["group_name"]);
treenode5.Value = pubfunc.ObjectToString(dr5["group_code"]);
treenode4.ChildNodes.Add(treenode5);
}
}
}
}
}
}
protected void btGetTreeview_Click(object sender, EventArgs e)
{
TreeView1.Nodes.Clear();
TreeView1.Visible = true;
DataSet ds = pkhead.readData("select * from [group] where group_code='00'");
if (ds.Tables.Count == 0) return;
foreach (DataRow dr in ds.Tables[0].Rows)
{
TreeNode treenode = new TreeNode();
treenode.Text = pubfunc.ObjectToString(dr["group_name"]);
treenode.Value = pubfunc.ObjectToString(dr["group_code"]);
TreeView1.Nodes.Add(treenode);
string sql2 = string.Format("select * from [group] where group_code like '{0}%' and len=2 order by group_code", treenode.Value);
DataSet ds2 = pkhead.readData(sql2);
if (ds2.Tables.Count == 0) return;
foreach (DataRow dr2 in ds2.Tables[0].Rows)
{
TreeNode treenode2 = new TreeNode();
treenode2.Text = pubfunc.ObjectToString(dr2["group_name"]);
treenode2.Value = pubfunc.ObjectToString(dr2["group_code"]);
treenode.ChildNodes.Add(treenode2);
string sql3 = string.Format("select * from [group] where group_code like '{0}%' and len=3 order by group_code", treenode2.Value);
DataSet ds3 = pkhead.readData(sql3);
if (ds3.Tables.Count == 0) return;
foreach (DataRow dr3 in ds3.Tables[0].Rows)
{
TreeNode treenode3 = new TreeNode();
treenode3.Text = pubfunc.ObjectToString(dr3["group_name"]);
treenode3.Value = pubfunc.ObjectToString(dr3["group_code"]);
treenode2.ChildNodes.Add(treenode3);
string sql4 = string.Format("select * from [group] where group_code like '{0}%' and len=4 order by group_code", treenode3.Value);
DataSet ds4 = pkhead.readData(sql4);
if (ds4.Tables.Count == 0) return;
foreach (DataRow dr4 in ds4.Tables[0].Rows)
{
TreeNode treenode4 = new TreeNode();
treenode4.Text = pubfunc.ObjectToString(dr4["group_name"]);
treenode4.Value = pubfunc.ObjectToString(dr4["group_code"]);
treenode3.ChildNodes.Add(treenode4);
string sql5 = string.Format("select * from [group] where group_code like '{0}%' and len=5 order by group_code", treenode4.Value);
DataSet ds5 = pkhead.readData(sql5);
if (ds4.Tables.Count == 0) return;
foreach (DataRow dr5 in ds5.Tables[0].Rows)
{
TreeNode treenode5 = new TreeNode();
treenode5.Text = pubfunc.ObjectToString(dr5["group_name"]);
treenode5.Value = pubfunc.ObjectToString(dr5["group_code"]);
treenode4.ChildNodes.Add(treenode5);
}
}
}
}
}
}