一颗两层节点完全展开的TreeView树
<div>
<table id ="tbl1" runat="server" >
<tr>
<td>
<asp:TreeView ID ="treeVType" runat ="server" ExpandDepth="1" OnSelectedNodeChanged="treeVType_SelectedNodeChanged">
</asp:TreeView>
</td>
</tr>
</table>
</div>
后台
///绑定树
private void TreeDpTree()
{
treeVType.Nodes.Clear();
Desktop.BLL.Type newtype = new Desktop.BLL.Type();
DataSet ds = newtype.GetTypeWare();
DataTable dt = ds.Tables[0];
string typecode ="";
TreeNode nodeParent = new TreeNode();
TreeNode nodeRoot=new TreeNode() ;
TreeNode node;
TreeNode node3 = new TreeNode();
if (dt.Rows.Count > 0)
{
nodeRoot.Text = "\\";
nodeRoot.Value="";
nodeRoot.Checked = true;
foreach (DataRow var in dt.Rows)
{
if (typecode != var[0].ToString())
{
if (typecode != "")
{
nodeRoot.ChildNodes.Add(nodeParent);
typecode = var[0].ToString();
nodeParent = new TreeNode();
}
nodeParent.Value = var["cType_code"].ToString();
nodeParent.Text = var["cType_name"].ToString();
typecode = var["cType_code"].ToString();
node = new TreeNode();
node.Value = var["cWare_code"].ToString();
node.Text = var["cWare_name"].ToString();
nodeParent.ChildNodes.Add(node);
}
else
{
node = new TreeNode();
node.Value = var["cWare_code"].ToString();
node.Text = var["cWare_name"].ToString();
nodeParent.ChildNodes.Add(node);
}
}
nodeRoot.ChildNodes.Add(nodeParent);
}
treeVType.Nodes.Add(nodeRoot);
treeVType.ExpandAll();
}