id p_id(父结果编号) name
怎样动态读取数据库生成一棵树型目录,用treeView实现,初学c#。
--------------------------------
12 个解决方案
#1
用递归最简单
1.先添加父结点,
2.然后根据ID找所有二级结点,
3.再循环,每个找到的结点重复第一步
不用递归也可,要搞一个数组存放所有未处理结点,麻烦不过效率高
1.先添加父结点,
2.然后根据ID找所有二级结点,
3.再循环,每个找到的结点重复第一步
不用递归也可,要搞一个数组存放所有未处理结点,麻烦不过效率高
#2
学习一下
#3
用递归最简单
1.先添加父结点,
2.然后根据ID找所有二级结点,
3.再循环,每个找到的结点重复第一步
这个方法
1.先添加父结点,
2.然后根据ID找所有二级结点,
3.再循环,每个找到的结点重复第一步
这个方法
#4
//DataTable dt
//DataRow dr;
//读取数据省略,下面是添加和找父级的过程,两个for,前一个加载,后一个找父级。
//其中dr中三列分别是ID,ParentID,Name
for (i = 0; i < dt.Rows.Count; i++)
{
dr = dt.Rows[i];
dr.ItemArray.GetValue(0).ToString();
tvwArea.Nodes.Add("key"+dr.ItemArray.GetValue(0).ToString(), dr.ItemArray.GetValue(2).ToString());
}
for (i = 0; i < dt.Rows.Count; i++)
{
dr = dt.Rows[i];
if (dr.ItemArray.GetValue(1).ToString != "")
{
tvwArea.Nodes["key"+dr.ItemArray.GetValue(0).ToString()].Parent = tvwArea.Nodes["key"+dr.ItemArray.GetValue(1).ToString()]
}
}
//DataRow dr;
//读取数据省略,下面是添加和找父级的过程,两个for,前一个加载,后一个找父级。
//其中dr中三列分别是ID,ParentID,Name
for (i = 0; i < dt.Rows.Count; i++)
{
dr = dt.Rows[i];
dr.ItemArray.GetValue(0).ToString();
tvwArea.Nodes.Add("key"+dr.ItemArray.GetValue(0).ToString(), dr.ItemArray.GetValue(2).ToString());
}
for (i = 0; i < dt.Rows.Count; i++)
{
dr = dt.Rows[i];
if (dr.ItemArray.GetValue(1).ToString != "")
{
tvwArea.Nodes["key"+dr.ItemArray.GetValue(0).ToString()].Parent = tvwArea.Nodes["key"+dr.ItemArray.GetValue(1).ToString()]
}
}
#5
//DataTable dt
//DataRow dr;
//读取数据省略,下面是添加和找父级的过程,两个for,前一个加载,后一个找父级。
//其中dr中三列分别是ID,ParentID,Name
//上面多写了一行。
for (i = 0; i < dt.Rows.Count; i++)
{
dr = dt.Rows[i];
tvwArea.Nodes.Add("key"+dr.ItemArray.GetValue(0).ToString(), dr.ItemArray.GetValue(2).ToString());
}
for (i = 0; i < dt.Rows.Count; i++)
{
dr = dt.Rows[i];
if (dr.ItemArray.GetValue(1).ToString != "")
{
tvwArea.Nodes["key"+dr.ItemArray.GetValue(0).ToString()].Parent = tvwArea.Nodes["key"+dr.ItemArray.GetValue(1).ToString()]
}
}
//DataRow dr;
//读取数据省略,下面是添加和找父级的过程,两个for,前一个加载,后一个找父级。
//其中dr中三列分别是ID,ParentID,Name
//上面多写了一行。
for (i = 0; i < dt.Rows.Count; i++)
{
dr = dt.Rows[i];
tvwArea.Nodes.Add("key"+dr.ItemArray.GetValue(0).ToString(), dr.ItemArray.GetValue(2).ToString());
}
for (i = 0; i < dt.Rows.Count; i++)
{
dr = dt.Rows[i];
if (dr.ItemArray.GetValue(1).ToString != "")
{
tvwArea.Nodes["key"+dr.ItemArray.GetValue(0).ToString()].Parent = tvwArea.Nodes["key"+dr.ItemArray.GetValue(1).ToString()]
}
}
#6
//--给你个例子
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
private string connstring = ConfigurationManager.ConnectionStrings["ConnStr"].ToString();
private bool isHaveCheckBox = false;
private bool isExpanded = false;
private DataTable dtTree = null;
protected void Page_Load(object sender, EventArgs e)
{
//Response.Write("tttttttttt");
if (!IsPostBack)
{
AreaTree.Nodes.Add(getRootAreaTreeNode(false, false));
}
}
/// <summary>
/// 获取包含全部子节点的根节点数据
/// </summary>
/// <param name="haveCheckBox">节点是否产生CheckBox</param>
/// <param name="expanded">节点是否展开</param>
/// <returns></returns>
public TreeNode getRootAreaTreeNode(bool haveCheckBox, bool expanded)
{
//drPeng.HS.BR.AreaTree brTree = new drPeng.HS.BR.AreaTree();
string rootID = "0001";//brTree.getRootAreaTreeID();
string rootName = "中国";//brTree.getRootAreaTreeName();
//ds = brTree.getAllAreaTree();
isHaveCheckBox = haveCheckBox;
isExpanded = expanded;
#region populate root node
TreeNode rootNode = new TreeNode();
rootNode.Text = rootName;
rootNode.Value = rootID;
//rootNode. = rootName;
// rootNode.NodeData = "0001";
rootNode.Expanded = true;
// rootNode.CheckBox = isHaveCheckBox;
#endregion
DataSet dsTree = SqlHelper.ExecuteDataset(connstring, CommandType.Text, "select id, p_id, name from YourTable");
dtTree = dsTree.Tables[0];
this.populateAreaTree(rootID, rootNode);
return rootNode;
}
private void populateAreaTree(string parentID, TreeNode pNode)
{
DataRow[] dRows = dtTree.Select("p_id=" + parentID);
if (dRows.Length > 0)
{
TreeNode Node = null;
foreach (DataRow drow in dRows)
{
Node = new TreeNode();
Node.Text = drow["Name"].ToString();
Node.Value = drow["ID"].ToString();
//Node.DataItem = Node.Text + "|" + Node.Value;
Node.Expanded = isExpanded;
pNode.ChildNodes.Add(Node);
populateAreaTree(Node.Value, Node); //递归
}
}
}
}
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
private string connstring = ConfigurationManager.ConnectionStrings["ConnStr"].ToString();
private bool isHaveCheckBox = false;
private bool isExpanded = false;
private DataTable dtTree = null;
protected void Page_Load(object sender, EventArgs e)
{
//Response.Write("tttttttttt");
if (!IsPostBack)
{
AreaTree.Nodes.Add(getRootAreaTreeNode(false, false));
}
}
/// <summary>
/// 获取包含全部子节点的根节点数据
/// </summary>
/// <param name="haveCheckBox">节点是否产生CheckBox</param>
/// <param name="expanded">节点是否展开</param>
/// <returns></returns>
public TreeNode getRootAreaTreeNode(bool haveCheckBox, bool expanded)
{
//drPeng.HS.BR.AreaTree brTree = new drPeng.HS.BR.AreaTree();
string rootID = "0001";//brTree.getRootAreaTreeID();
string rootName = "中国";//brTree.getRootAreaTreeName();
//ds = brTree.getAllAreaTree();
isHaveCheckBox = haveCheckBox;
isExpanded = expanded;
#region populate root node
TreeNode rootNode = new TreeNode();
rootNode.Text = rootName;
rootNode.Value = rootID;
//rootNode. = rootName;
// rootNode.NodeData = "0001";
rootNode.Expanded = true;
// rootNode.CheckBox = isHaveCheckBox;
#endregion
DataSet dsTree = SqlHelper.ExecuteDataset(connstring, CommandType.Text, "select id, p_id, name from YourTable");
dtTree = dsTree.Tables[0];
this.populateAreaTree(rootID, rootNode);
return rootNode;
}
private void populateAreaTree(string parentID, TreeNode pNode)
{
DataRow[] dRows = dtTree.Select("p_id=" + parentID);
if (dRows.Length > 0)
{
TreeNode Node = null;
foreach (DataRow drow in dRows)
{
Node = new TreeNode();
Node.Text = drow["Name"].ToString();
Node.Value = drow["ID"].ToString();
//Node.DataItem = Node.Text + "|" + Node.Value;
Node.Expanded = isExpanded;
pNode.ChildNodes.Add(Node);
populateAreaTree(Node.Value, Node); //递归
}
}
}
}
#7
刚看了一下,.net中node.parent是只读,我上面写的方法无效。
#8
学习
#9
mark
#10
/// <summary>
/// 简单类型接口
/// </summary>
public interface ISimpleType
{
SimpleTypeInfoCollection getTreeTypes();
}
/// <summary>
/// 简单类型信息
/// </summary>
public class SimpleTypeInfo
{
private int simpleTypeId;
private string typeName=string.Empty;
private int parentId;
private SimpleTypeInfo parentInfo=null;
private SimpleTypeInfoCollection childs=null;
public int SimpleTypeID
{
get{return simpleTypeId;}
set{simpleTypeId=value;}
}
public string TypeName
{
get{return typeName;}
set{typeName=value;}
}
public int ParentID
{
get{return parentId;}
set{parentId=value;}
}
public SimpleTypeInfo ParentInfo
{
get{return parentInfo;}
set{parentInfo=value;}
}
public SimpleTypeInfoCollection Childs
{
get{return childs;}
set{childs=value;}
}
public SimpleTypeInfo(){}
public SimpleTypeInfo(int simpleTypeId,string typeName,int parentId,SimpleTypeInfo parentInfo,SimpleTypeInfoCollection childs)
{
this.simpleTypeId=simpleTypeId;
this.typeName=typeName;
this.parentId=parentId;
this.parentInfo=parentInfo;
this.childs=childs;
}
}
/// <summary>
/// 简单类型信息集合
/// </summary>
public class SimpleTypeInfoCollection:CollectionBase
{
public SimpleTypeInfoCollection(){}
public SimpleTypeInfo this[int index]
{
get{return (SimpleTypeInfo)List[index];}
set{List[index]=value;}
}
public int Add(SimpleTypeInfo value)
{
return List.Add(value);
}
public int IndexOf(SimpleTypeInfo value)
{
return List.IndexOf(value);
}
public void Insert(int index,SimpleTypeInfo value)
{
List.Insert(index,value);
}
public void Remove(SimpleTypeInfo value)
{
List.Remove(value);
}
public bool Contains(SimpleTypeInfo value)
{
return List.Contains(value);
}
}
/// 简单类型接口
/// </summary>
public interface ISimpleType
{
SimpleTypeInfoCollection getTreeTypes();
}
/// <summary>
/// 简单类型信息
/// </summary>
public class SimpleTypeInfo
{
private int simpleTypeId;
private string typeName=string.Empty;
private int parentId;
private SimpleTypeInfo parentInfo=null;
private SimpleTypeInfoCollection childs=null;
public int SimpleTypeID
{
get{return simpleTypeId;}
set{simpleTypeId=value;}
}
public string TypeName
{
get{return typeName;}
set{typeName=value;}
}
public int ParentID
{
get{return parentId;}
set{parentId=value;}
}
public SimpleTypeInfo ParentInfo
{
get{return parentInfo;}
set{parentInfo=value;}
}
public SimpleTypeInfoCollection Childs
{
get{return childs;}
set{childs=value;}
}
public SimpleTypeInfo(){}
public SimpleTypeInfo(int simpleTypeId,string typeName,int parentId,SimpleTypeInfo parentInfo,SimpleTypeInfoCollection childs)
{
this.simpleTypeId=simpleTypeId;
this.typeName=typeName;
this.parentId=parentId;
this.parentInfo=parentInfo;
this.childs=childs;
}
}
/// <summary>
/// 简单类型信息集合
/// </summary>
public class SimpleTypeInfoCollection:CollectionBase
{
public SimpleTypeInfoCollection(){}
public SimpleTypeInfo this[int index]
{
get{return (SimpleTypeInfo)List[index];}
set{List[index]=value;}
}
public int Add(SimpleTypeInfo value)
{
return List.Add(value);
}
public int IndexOf(SimpleTypeInfo value)
{
return List.IndexOf(value);
}
public void Insert(int index,SimpleTypeInfo value)
{
List.Insert(index,value);
}
public void Remove(SimpleTypeInfo value)
{
List.Remove(value);
}
public bool Contains(SimpleTypeInfo value)
{
return List.Contains(value);
}
}
#11
/// <summary>
/// SqlServer实现类
/// </summary>
public class SqlSimpleType:ISimpleType
{
protected string connectionString=@"database=yourdb;server=yourserver;user id=username;password=password;Pooling=true";
public SqlSimpleType(){}
#region ISimpleType 成员
/// <summary>
/// 返回树对象
/// </summary>
/// <returns></returns>
public SimpleTypeInfoCollection getTreeTypes()
{
DataTable dt=this.getSimpleTypes(connectionString);
if(dt==null)
return null;
IDictionary all=new Hashtable();
for(int n=0;n<dt.Rows.Count;n++)
{
SimpleTypeInfo info=this.ConvertType(dt.Rows[n]);
if(info!=null)
all.Add(info.SimpleTypeID,info);
}
SimpleTypeInfoCollection types=this.CreateTreeStructure(all);
return types;
}
/// <summary>
/// 根据Hash创建树结构对象
/// </summary>
/// <param name="all"></param>
/// <returns></returns>
protected SimpleTypeInfoCollection CreateTreeStructure(IDictionary all)
{
if(all==null||all.Count<=0)
return null;
IDictionaryEnumerator de=all.GetEnumerator();
SimpleTypeInfoCollection collection=new SimpleTypeInfoCollection();
while(de.MoveNext())
{
SimpleTypeInfo info=(SimpleTypeInfo)de.Value;
//只添加根
if(info.ParentID<=0)
collection.Add(info);
if(all.Contains(info.ParentID))
{
SimpleTypeInfo parentInfo=((SimpleTypeInfo)all[info.ParentID]);
if(parentInfo.Childs==null)
parentInfo.Childs=new SimpleTypeInfoCollection();
parentInfo.Childs.Add(info);
info.ParentInfo=parentInfo;
}
}
return collection;
}
/// <summary>
/// 返回简单类型表中的所有数据
/// </summary>
/// <param name="url"></param>
/// <returns></returns>
protected DataTable getSimpleTypes(string url)
{
string sql="Select * From SimpleType";
SqlCommand cmd=new SqlCommand(sql,new SqlConnection(url));
SqlDataAdapter da=new SqlDataAdapter(cmd);
DataTable dt=new DataTable();
da.Fill(dt);
return dt;
}
/// <summary>
/// 转换简单类型信息
/// </summary>
/// <param name="dr"></param>
/// <returns></returns>
protected SimpleTypeInfo ConvertType(DataRow dr)
{
if(dr==null)
return null;
SimpleTypeInfo info=new SimpleTypeInfo();
info.SimpleTypeID=Convert.ToInt32(dr["SimpleTypeID"]);
info.TypeName=Convert.ToString(dr["TypeName"]);
info.ParentID=dr["ParentID"]==System.DBNull.Value?0:Convert.ToInt32(dr["ParentID"]);
return info;
}
#endregion
}
public class WebForm1 : System.Web.UI.Page
{
protected Microsoft.Web.UI.WebControls.TreeView testTree;
private void Page_Load(object sender, System.EventArgs e)
{
SqlSimpleType s=new SqlSimpleType();
SimpleTypeInfoCollection collection=s.getTreeTypes();
if(collection!=null)
collection.Count.ToString();
testTree.Nodes.Clear();
this.CreateItems(collection,testTree.Nodes);
}
private void CreateItems(SimpleTypeInfoCollection items,TreeNodeCollection nodes)
{
if(items==null||items.Count<=0)
return;
foreach(SimpleTypeInfo info in items)
this.CreateItem(info,nodes);
}
private void CreateItem(SimpleTypeInfo info,TreeNodeCollection nodes)
{
if(info!=null)
{
TreeNode node=new TreeNode();
node.Text=info.TypeName;
nodes.Add(node);
this.CreateItems(info.Childs,nodes[nodes.Count-1].Nodes);
}
}
/// SqlServer实现类
/// </summary>
public class SqlSimpleType:ISimpleType
{
protected string connectionString=@"database=yourdb;server=yourserver;user id=username;password=password;Pooling=true";
public SqlSimpleType(){}
#region ISimpleType 成员
/// <summary>
/// 返回树对象
/// </summary>
/// <returns></returns>
public SimpleTypeInfoCollection getTreeTypes()
{
DataTable dt=this.getSimpleTypes(connectionString);
if(dt==null)
return null;
IDictionary all=new Hashtable();
for(int n=0;n<dt.Rows.Count;n++)
{
SimpleTypeInfo info=this.ConvertType(dt.Rows[n]);
if(info!=null)
all.Add(info.SimpleTypeID,info);
}
SimpleTypeInfoCollection types=this.CreateTreeStructure(all);
return types;
}
/// <summary>
/// 根据Hash创建树结构对象
/// </summary>
/// <param name="all"></param>
/// <returns></returns>
protected SimpleTypeInfoCollection CreateTreeStructure(IDictionary all)
{
if(all==null||all.Count<=0)
return null;
IDictionaryEnumerator de=all.GetEnumerator();
SimpleTypeInfoCollection collection=new SimpleTypeInfoCollection();
while(de.MoveNext())
{
SimpleTypeInfo info=(SimpleTypeInfo)de.Value;
//只添加根
if(info.ParentID<=0)
collection.Add(info);
if(all.Contains(info.ParentID))
{
SimpleTypeInfo parentInfo=((SimpleTypeInfo)all[info.ParentID]);
if(parentInfo.Childs==null)
parentInfo.Childs=new SimpleTypeInfoCollection();
parentInfo.Childs.Add(info);
info.ParentInfo=parentInfo;
}
}
return collection;
}
/// <summary>
/// 返回简单类型表中的所有数据
/// </summary>
/// <param name="url"></param>
/// <returns></returns>
protected DataTable getSimpleTypes(string url)
{
string sql="Select * From SimpleType";
SqlCommand cmd=new SqlCommand(sql,new SqlConnection(url));
SqlDataAdapter da=new SqlDataAdapter(cmd);
DataTable dt=new DataTable();
da.Fill(dt);
return dt;
}
/// <summary>
/// 转换简单类型信息
/// </summary>
/// <param name="dr"></param>
/// <returns></returns>
protected SimpleTypeInfo ConvertType(DataRow dr)
{
if(dr==null)
return null;
SimpleTypeInfo info=new SimpleTypeInfo();
info.SimpleTypeID=Convert.ToInt32(dr["SimpleTypeID"]);
info.TypeName=Convert.ToString(dr["TypeName"]);
info.ParentID=dr["ParentID"]==System.DBNull.Value?0:Convert.ToInt32(dr["ParentID"]);
return info;
}
#endregion
}
public class WebForm1 : System.Web.UI.Page
{
protected Microsoft.Web.UI.WebControls.TreeView testTree;
private void Page_Load(object sender, System.EventArgs e)
{
SqlSimpleType s=new SqlSimpleType();
SimpleTypeInfoCollection collection=s.getTreeTypes();
if(collection!=null)
collection.Count.ToString();
testTree.Nodes.Clear();
this.CreateItems(collection,testTree.Nodes);
}
private void CreateItems(SimpleTypeInfoCollection items,TreeNodeCollection nodes)
{
if(items==null||items.Count<=0)
return;
foreach(SimpleTypeInfo info in items)
this.CreateItem(info,nodes);
}
private void CreateItem(SimpleTypeInfo info,TreeNodeCollection nodes)
{
if(info!=null)
{
TreeNode node=new TreeNode();
node.Text=info.TypeName;
nodes.Add(node);
this.CreateItems(info.Childs,nodes[nodes.Count-1].Nodes);
}
}
#12
如果你不愿意写自己的信息类,你可以参考WebForm1直接操作TreeView
#1
用递归最简单
1.先添加父结点,
2.然后根据ID找所有二级结点,
3.再循环,每个找到的结点重复第一步
不用递归也可,要搞一个数组存放所有未处理结点,麻烦不过效率高
1.先添加父结点,
2.然后根据ID找所有二级结点,
3.再循环,每个找到的结点重复第一步
不用递归也可,要搞一个数组存放所有未处理结点,麻烦不过效率高
#2
学习一下
#3
用递归最简单
1.先添加父结点,
2.然后根据ID找所有二级结点,
3.再循环,每个找到的结点重复第一步
这个方法
1.先添加父结点,
2.然后根据ID找所有二级结点,
3.再循环,每个找到的结点重复第一步
这个方法
#4
//DataTable dt
//DataRow dr;
//读取数据省略,下面是添加和找父级的过程,两个for,前一个加载,后一个找父级。
//其中dr中三列分别是ID,ParentID,Name
for (i = 0; i < dt.Rows.Count; i++)
{
dr = dt.Rows[i];
dr.ItemArray.GetValue(0).ToString();
tvwArea.Nodes.Add("key"+dr.ItemArray.GetValue(0).ToString(), dr.ItemArray.GetValue(2).ToString());
}
for (i = 0; i < dt.Rows.Count; i++)
{
dr = dt.Rows[i];
if (dr.ItemArray.GetValue(1).ToString != "")
{
tvwArea.Nodes["key"+dr.ItemArray.GetValue(0).ToString()].Parent = tvwArea.Nodes["key"+dr.ItemArray.GetValue(1).ToString()]
}
}
//DataRow dr;
//读取数据省略,下面是添加和找父级的过程,两个for,前一个加载,后一个找父级。
//其中dr中三列分别是ID,ParentID,Name
for (i = 0; i < dt.Rows.Count; i++)
{
dr = dt.Rows[i];
dr.ItemArray.GetValue(0).ToString();
tvwArea.Nodes.Add("key"+dr.ItemArray.GetValue(0).ToString(), dr.ItemArray.GetValue(2).ToString());
}
for (i = 0; i < dt.Rows.Count; i++)
{
dr = dt.Rows[i];
if (dr.ItemArray.GetValue(1).ToString != "")
{
tvwArea.Nodes["key"+dr.ItemArray.GetValue(0).ToString()].Parent = tvwArea.Nodes["key"+dr.ItemArray.GetValue(1).ToString()]
}
}
#5
//DataTable dt
//DataRow dr;
//读取数据省略,下面是添加和找父级的过程,两个for,前一个加载,后一个找父级。
//其中dr中三列分别是ID,ParentID,Name
//上面多写了一行。
for (i = 0; i < dt.Rows.Count; i++)
{
dr = dt.Rows[i];
tvwArea.Nodes.Add("key"+dr.ItemArray.GetValue(0).ToString(), dr.ItemArray.GetValue(2).ToString());
}
for (i = 0; i < dt.Rows.Count; i++)
{
dr = dt.Rows[i];
if (dr.ItemArray.GetValue(1).ToString != "")
{
tvwArea.Nodes["key"+dr.ItemArray.GetValue(0).ToString()].Parent = tvwArea.Nodes["key"+dr.ItemArray.GetValue(1).ToString()]
}
}
//DataRow dr;
//读取数据省略,下面是添加和找父级的过程,两个for,前一个加载,后一个找父级。
//其中dr中三列分别是ID,ParentID,Name
//上面多写了一行。
for (i = 0; i < dt.Rows.Count; i++)
{
dr = dt.Rows[i];
tvwArea.Nodes.Add("key"+dr.ItemArray.GetValue(0).ToString(), dr.ItemArray.GetValue(2).ToString());
}
for (i = 0; i < dt.Rows.Count; i++)
{
dr = dt.Rows[i];
if (dr.ItemArray.GetValue(1).ToString != "")
{
tvwArea.Nodes["key"+dr.ItemArray.GetValue(0).ToString()].Parent = tvwArea.Nodes["key"+dr.ItemArray.GetValue(1).ToString()]
}
}
#6
//--给你个例子
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
private string connstring = ConfigurationManager.ConnectionStrings["ConnStr"].ToString();
private bool isHaveCheckBox = false;
private bool isExpanded = false;
private DataTable dtTree = null;
protected void Page_Load(object sender, EventArgs e)
{
//Response.Write("tttttttttt");
if (!IsPostBack)
{
AreaTree.Nodes.Add(getRootAreaTreeNode(false, false));
}
}
/// <summary>
/// 获取包含全部子节点的根节点数据
/// </summary>
/// <param name="haveCheckBox">节点是否产生CheckBox</param>
/// <param name="expanded">节点是否展开</param>
/// <returns></returns>
public TreeNode getRootAreaTreeNode(bool haveCheckBox, bool expanded)
{
//drPeng.HS.BR.AreaTree brTree = new drPeng.HS.BR.AreaTree();
string rootID = "0001";//brTree.getRootAreaTreeID();
string rootName = "中国";//brTree.getRootAreaTreeName();
//ds = brTree.getAllAreaTree();
isHaveCheckBox = haveCheckBox;
isExpanded = expanded;
#region populate root node
TreeNode rootNode = new TreeNode();
rootNode.Text = rootName;
rootNode.Value = rootID;
//rootNode. = rootName;
// rootNode.NodeData = "0001";
rootNode.Expanded = true;
// rootNode.CheckBox = isHaveCheckBox;
#endregion
DataSet dsTree = SqlHelper.ExecuteDataset(connstring, CommandType.Text, "select id, p_id, name from YourTable");
dtTree = dsTree.Tables[0];
this.populateAreaTree(rootID, rootNode);
return rootNode;
}
private void populateAreaTree(string parentID, TreeNode pNode)
{
DataRow[] dRows = dtTree.Select("p_id=" + parentID);
if (dRows.Length > 0)
{
TreeNode Node = null;
foreach (DataRow drow in dRows)
{
Node = new TreeNode();
Node.Text = drow["Name"].ToString();
Node.Value = drow["ID"].ToString();
//Node.DataItem = Node.Text + "|" + Node.Value;
Node.Expanded = isExpanded;
pNode.ChildNodes.Add(Node);
populateAreaTree(Node.Value, Node); //递归
}
}
}
}
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
private string connstring = ConfigurationManager.ConnectionStrings["ConnStr"].ToString();
private bool isHaveCheckBox = false;
private bool isExpanded = false;
private DataTable dtTree = null;
protected void Page_Load(object sender, EventArgs e)
{
//Response.Write("tttttttttt");
if (!IsPostBack)
{
AreaTree.Nodes.Add(getRootAreaTreeNode(false, false));
}
}
/// <summary>
/// 获取包含全部子节点的根节点数据
/// </summary>
/// <param name="haveCheckBox">节点是否产生CheckBox</param>
/// <param name="expanded">节点是否展开</param>
/// <returns></returns>
public TreeNode getRootAreaTreeNode(bool haveCheckBox, bool expanded)
{
//drPeng.HS.BR.AreaTree brTree = new drPeng.HS.BR.AreaTree();
string rootID = "0001";//brTree.getRootAreaTreeID();
string rootName = "中国";//brTree.getRootAreaTreeName();
//ds = brTree.getAllAreaTree();
isHaveCheckBox = haveCheckBox;
isExpanded = expanded;
#region populate root node
TreeNode rootNode = new TreeNode();
rootNode.Text = rootName;
rootNode.Value = rootID;
//rootNode. = rootName;
// rootNode.NodeData = "0001";
rootNode.Expanded = true;
// rootNode.CheckBox = isHaveCheckBox;
#endregion
DataSet dsTree = SqlHelper.ExecuteDataset(connstring, CommandType.Text, "select id, p_id, name from YourTable");
dtTree = dsTree.Tables[0];
this.populateAreaTree(rootID, rootNode);
return rootNode;
}
private void populateAreaTree(string parentID, TreeNode pNode)
{
DataRow[] dRows = dtTree.Select("p_id=" + parentID);
if (dRows.Length > 0)
{
TreeNode Node = null;
foreach (DataRow drow in dRows)
{
Node = new TreeNode();
Node.Text = drow["Name"].ToString();
Node.Value = drow["ID"].ToString();
//Node.DataItem = Node.Text + "|" + Node.Value;
Node.Expanded = isExpanded;
pNode.ChildNodes.Add(Node);
populateAreaTree(Node.Value, Node); //递归
}
}
}
}
#7
刚看了一下,.net中node.parent是只读,我上面写的方法无效。
#8
学习
#9
mark
#10
/// <summary>
/// 简单类型接口
/// </summary>
public interface ISimpleType
{
SimpleTypeInfoCollection getTreeTypes();
}
/// <summary>
/// 简单类型信息
/// </summary>
public class SimpleTypeInfo
{
private int simpleTypeId;
private string typeName=string.Empty;
private int parentId;
private SimpleTypeInfo parentInfo=null;
private SimpleTypeInfoCollection childs=null;
public int SimpleTypeID
{
get{return simpleTypeId;}
set{simpleTypeId=value;}
}
public string TypeName
{
get{return typeName;}
set{typeName=value;}
}
public int ParentID
{
get{return parentId;}
set{parentId=value;}
}
public SimpleTypeInfo ParentInfo
{
get{return parentInfo;}
set{parentInfo=value;}
}
public SimpleTypeInfoCollection Childs
{
get{return childs;}
set{childs=value;}
}
public SimpleTypeInfo(){}
public SimpleTypeInfo(int simpleTypeId,string typeName,int parentId,SimpleTypeInfo parentInfo,SimpleTypeInfoCollection childs)
{
this.simpleTypeId=simpleTypeId;
this.typeName=typeName;
this.parentId=parentId;
this.parentInfo=parentInfo;
this.childs=childs;
}
}
/// <summary>
/// 简单类型信息集合
/// </summary>
public class SimpleTypeInfoCollection:CollectionBase
{
public SimpleTypeInfoCollection(){}
public SimpleTypeInfo this[int index]
{
get{return (SimpleTypeInfo)List[index];}
set{List[index]=value;}
}
public int Add(SimpleTypeInfo value)
{
return List.Add(value);
}
public int IndexOf(SimpleTypeInfo value)
{
return List.IndexOf(value);
}
public void Insert(int index,SimpleTypeInfo value)
{
List.Insert(index,value);
}
public void Remove(SimpleTypeInfo value)
{
List.Remove(value);
}
public bool Contains(SimpleTypeInfo value)
{
return List.Contains(value);
}
}
/// 简单类型接口
/// </summary>
public interface ISimpleType
{
SimpleTypeInfoCollection getTreeTypes();
}
/// <summary>
/// 简单类型信息
/// </summary>
public class SimpleTypeInfo
{
private int simpleTypeId;
private string typeName=string.Empty;
private int parentId;
private SimpleTypeInfo parentInfo=null;
private SimpleTypeInfoCollection childs=null;
public int SimpleTypeID
{
get{return simpleTypeId;}
set{simpleTypeId=value;}
}
public string TypeName
{
get{return typeName;}
set{typeName=value;}
}
public int ParentID
{
get{return parentId;}
set{parentId=value;}
}
public SimpleTypeInfo ParentInfo
{
get{return parentInfo;}
set{parentInfo=value;}
}
public SimpleTypeInfoCollection Childs
{
get{return childs;}
set{childs=value;}
}
public SimpleTypeInfo(){}
public SimpleTypeInfo(int simpleTypeId,string typeName,int parentId,SimpleTypeInfo parentInfo,SimpleTypeInfoCollection childs)
{
this.simpleTypeId=simpleTypeId;
this.typeName=typeName;
this.parentId=parentId;
this.parentInfo=parentInfo;
this.childs=childs;
}
}
/// <summary>
/// 简单类型信息集合
/// </summary>
public class SimpleTypeInfoCollection:CollectionBase
{
public SimpleTypeInfoCollection(){}
public SimpleTypeInfo this[int index]
{
get{return (SimpleTypeInfo)List[index];}
set{List[index]=value;}
}
public int Add(SimpleTypeInfo value)
{
return List.Add(value);
}
public int IndexOf(SimpleTypeInfo value)
{
return List.IndexOf(value);
}
public void Insert(int index,SimpleTypeInfo value)
{
List.Insert(index,value);
}
public void Remove(SimpleTypeInfo value)
{
List.Remove(value);
}
public bool Contains(SimpleTypeInfo value)
{
return List.Contains(value);
}
}
#11
/// <summary>
/// SqlServer实现类
/// </summary>
public class SqlSimpleType:ISimpleType
{
protected string connectionString=@"database=yourdb;server=yourserver;user id=username;password=password;Pooling=true";
public SqlSimpleType(){}
#region ISimpleType 成员
/// <summary>
/// 返回树对象
/// </summary>
/// <returns></returns>
public SimpleTypeInfoCollection getTreeTypes()
{
DataTable dt=this.getSimpleTypes(connectionString);
if(dt==null)
return null;
IDictionary all=new Hashtable();
for(int n=0;n<dt.Rows.Count;n++)
{
SimpleTypeInfo info=this.ConvertType(dt.Rows[n]);
if(info!=null)
all.Add(info.SimpleTypeID,info);
}
SimpleTypeInfoCollection types=this.CreateTreeStructure(all);
return types;
}
/// <summary>
/// 根据Hash创建树结构对象
/// </summary>
/// <param name="all"></param>
/// <returns></returns>
protected SimpleTypeInfoCollection CreateTreeStructure(IDictionary all)
{
if(all==null||all.Count<=0)
return null;
IDictionaryEnumerator de=all.GetEnumerator();
SimpleTypeInfoCollection collection=new SimpleTypeInfoCollection();
while(de.MoveNext())
{
SimpleTypeInfo info=(SimpleTypeInfo)de.Value;
//只添加根
if(info.ParentID<=0)
collection.Add(info);
if(all.Contains(info.ParentID))
{
SimpleTypeInfo parentInfo=((SimpleTypeInfo)all[info.ParentID]);
if(parentInfo.Childs==null)
parentInfo.Childs=new SimpleTypeInfoCollection();
parentInfo.Childs.Add(info);
info.ParentInfo=parentInfo;
}
}
return collection;
}
/// <summary>
/// 返回简单类型表中的所有数据
/// </summary>
/// <param name="url"></param>
/// <returns></returns>
protected DataTable getSimpleTypes(string url)
{
string sql="Select * From SimpleType";
SqlCommand cmd=new SqlCommand(sql,new SqlConnection(url));
SqlDataAdapter da=new SqlDataAdapter(cmd);
DataTable dt=new DataTable();
da.Fill(dt);
return dt;
}
/// <summary>
/// 转换简单类型信息
/// </summary>
/// <param name="dr"></param>
/// <returns></returns>
protected SimpleTypeInfo ConvertType(DataRow dr)
{
if(dr==null)
return null;
SimpleTypeInfo info=new SimpleTypeInfo();
info.SimpleTypeID=Convert.ToInt32(dr["SimpleTypeID"]);
info.TypeName=Convert.ToString(dr["TypeName"]);
info.ParentID=dr["ParentID"]==System.DBNull.Value?0:Convert.ToInt32(dr["ParentID"]);
return info;
}
#endregion
}
public class WebForm1 : System.Web.UI.Page
{
protected Microsoft.Web.UI.WebControls.TreeView testTree;
private void Page_Load(object sender, System.EventArgs e)
{
SqlSimpleType s=new SqlSimpleType();
SimpleTypeInfoCollection collection=s.getTreeTypes();
if(collection!=null)
collection.Count.ToString();
testTree.Nodes.Clear();
this.CreateItems(collection,testTree.Nodes);
}
private void CreateItems(SimpleTypeInfoCollection items,TreeNodeCollection nodes)
{
if(items==null||items.Count<=0)
return;
foreach(SimpleTypeInfo info in items)
this.CreateItem(info,nodes);
}
private void CreateItem(SimpleTypeInfo info,TreeNodeCollection nodes)
{
if(info!=null)
{
TreeNode node=new TreeNode();
node.Text=info.TypeName;
nodes.Add(node);
this.CreateItems(info.Childs,nodes[nodes.Count-1].Nodes);
}
}
/// SqlServer实现类
/// </summary>
public class SqlSimpleType:ISimpleType
{
protected string connectionString=@"database=yourdb;server=yourserver;user id=username;password=password;Pooling=true";
public SqlSimpleType(){}
#region ISimpleType 成员
/// <summary>
/// 返回树对象
/// </summary>
/// <returns></returns>
public SimpleTypeInfoCollection getTreeTypes()
{
DataTable dt=this.getSimpleTypes(connectionString);
if(dt==null)
return null;
IDictionary all=new Hashtable();
for(int n=0;n<dt.Rows.Count;n++)
{
SimpleTypeInfo info=this.ConvertType(dt.Rows[n]);
if(info!=null)
all.Add(info.SimpleTypeID,info);
}
SimpleTypeInfoCollection types=this.CreateTreeStructure(all);
return types;
}
/// <summary>
/// 根据Hash创建树结构对象
/// </summary>
/// <param name="all"></param>
/// <returns></returns>
protected SimpleTypeInfoCollection CreateTreeStructure(IDictionary all)
{
if(all==null||all.Count<=0)
return null;
IDictionaryEnumerator de=all.GetEnumerator();
SimpleTypeInfoCollection collection=new SimpleTypeInfoCollection();
while(de.MoveNext())
{
SimpleTypeInfo info=(SimpleTypeInfo)de.Value;
//只添加根
if(info.ParentID<=0)
collection.Add(info);
if(all.Contains(info.ParentID))
{
SimpleTypeInfo parentInfo=((SimpleTypeInfo)all[info.ParentID]);
if(parentInfo.Childs==null)
parentInfo.Childs=new SimpleTypeInfoCollection();
parentInfo.Childs.Add(info);
info.ParentInfo=parentInfo;
}
}
return collection;
}
/// <summary>
/// 返回简单类型表中的所有数据
/// </summary>
/// <param name="url"></param>
/// <returns></returns>
protected DataTable getSimpleTypes(string url)
{
string sql="Select * From SimpleType";
SqlCommand cmd=new SqlCommand(sql,new SqlConnection(url));
SqlDataAdapter da=new SqlDataAdapter(cmd);
DataTable dt=new DataTable();
da.Fill(dt);
return dt;
}
/// <summary>
/// 转换简单类型信息
/// </summary>
/// <param name="dr"></param>
/// <returns></returns>
protected SimpleTypeInfo ConvertType(DataRow dr)
{
if(dr==null)
return null;
SimpleTypeInfo info=new SimpleTypeInfo();
info.SimpleTypeID=Convert.ToInt32(dr["SimpleTypeID"]);
info.TypeName=Convert.ToString(dr["TypeName"]);
info.ParentID=dr["ParentID"]==System.DBNull.Value?0:Convert.ToInt32(dr["ParentID"]);
return info;
}
#endregion
}
public class WebForm1 : System.Web.UI.Page
{
protected Microsoft.Web.UI.WebControls.TreeView testTree;
private void Page_Load(object sender, System.EventArgs e)
{
SqlSimpleType s=new SqlSimpleType();
SimpleTypeInfoCollection collection=s.getTreeTypes();
if(collection!=null)
collection.Count.ToString();
testTree.Nodes.Clear();
this.CreateItems(collection,testTree.Nodes);
}
private void CreateItems(SimpleTypeInfoCollection items,TreeNodeCollection nodes)
{
if(items==null||items.Count<=0)
return;
foreach(SimpleTypeInfo info in items)
this.CreateItem(info,nodes);
}
private void CreateItem(SimpleTypeInfo info,TreeNodeCollection nodes)
{
if(info!=null)
{
TreeNode node=new TreeNode();
node.Text=info.TypeName;
nodes.Add(node);
this.CreateItems(info.Childs,nodes[nodes.Count-1].Nodes);
}
}
#12
如果你不愿意写自己的信息类,你可以参考WebForm1直接操作TreeView