方法一:
#region "读取树结点从Datatable"
/// <summary>
/// 读取树结点从Datatable"
/// </summary>
/// <param name="TreeView1">在填充的TreeView控件</param>
/// <param name="DT">数据源DataTable</param>
/// <param name="IsAppendNode">是在现有TreeView控件上添加结点,还是清空再添加</param>
/// <param name="ParentNumberColumnIndex">在DataTable中,代表父节点编号的列索引</param>
/// <param name="NumberColumnIndex">在DataTable中,代表当前节点编号的列索引</param>
/// <param name="NameColumnIndex">在DataTable中,代表当前节点名称的列索引</param>
/// <returns>True/False</returns>
public bool ReadNodesFromDataTable(TreeView TreeView1, DataTable DT, bool IsAppendNode, int ParentNumberColumnIndex, int NumberColumnIndex, int NameColumnIndex)
{
try
{
if (IsAppendNode == false)
{
TreeView1.Nodes.Clear();
}
if (DT != null && DT.Rows.Count > 0)
{
DataRow[] DR = null;
DR = DT.Select(DT.Columns[ParentNumberColumnIndex].ColumnName + "='' or " + DT.Columns[ParentNumberColumnIndex].ColumnName + "='0' or " + DT.Columns[ParentNumberColumnIndex].ColumnName + " is null");//先将顶级的查出来
for (int I = 0; I <= DR.Length - 1; I++)//先将顶级的加入到TreeView中
{
TreeNode TNode = new TreeNode(DR[I][DT.Columns[NameColumnIndex].ColumnName].ToString());
TNode.Tag = DR[I][DT.Columns[NumberColumnIndex].ColumnName].ToString();
TNode.Name = DR[I][DT.Columns[NameColumnIndex].ColumnName].ToString();
TreeView1.Nodes.Add(TNode);
}
for (int I = 0; I <= TreeView1.Nodes.Count - 1; I++)//再递归遍历结点
{
ForTreeNodeFormDT(TreeView1.Nodes[I], DT, ParentNumberColumnIndex, NumberColumnIndex, NameColumnIndex);
}
}
return false;
}
catch
{
return true;
}
}
/// <summary>
/// 从DT中递归遍历出结点
/// </summary>
/// <param name="TempNode">传入的顶级结点</param>
/// <param name="DT">保存TreeView结构的DataTable</param>
/// <param name="ParentNumberColumnIndex">在DataTable中,代表父节点编号的列索引</param>
/// <param name="NumberColumnIndex">在DataTable中,代表当前节点编号的列索引</param>
/// <param name="NameColumnIndex">在DataTable中,代表当前节点名称的列索引</param>
private void ForTreeNodeFormDT(TreeNode TempNode, DataTable DT, int ParentNumberColumnIndex, int NumberColumnIndex, int NameColumnIndex)
{
string TTag = null;
TTag = TempNode.Tag.ToString();
DataRow[] DR = null;
DR = DT.Select(DT.Columns[ParentNumberColumnIndex].ColumnName + "='" + TTag + "'");
for (int I = 0; I <= DR.Length - 1; I++)
{
TreeNode TNode = new TreeNode(DR[I][DT.Columns[NameColumnIndex].ColumnName].ToString());
TNode.Tag = DR[I][DT.Columns[NumberColumnIndex].ColumnName].ToString();
TNode.Name = DR[I][DT.Columns[NameColumnIndex].ColumnName].ToString();
TempNode.Nodes.Add(TNode);
}
foreach (TreeNode aNode in TempNode.Nodes)
{
ForTreeNodeFormDT(aNode, DT, ParentNumberColumnIndex, NumberColumnIndex, NameColumnIndex);
}
}
#endregion
方法二:
做分类 经常会用到无限级别的分类 先介绍一下数据库的表结构
tid 类别编号
tname 类别名称
pid 父类编号
测试数据就不写了,大家可以自己插入一下试试
查询制定类别的 所有的子类 sql 的 代码
alter proc proc_chaxun (@tid int ) as begin with tt as ( select tid,tname,pid from dbo.t_goodsType where tid=@tid union all select t.tid,t.tname,t.pid from dbo.t_goodsType t inner join tt on t.pid=tt.tid ) select * from tt end
查询之后获取记录集 绑定到前台的 TreeView 上面
/// <summary> /// 给Tree 绑定数据 递归添加子节点 /// </summary> /// <param name="dv">数据视图</param> /// <param name="tnOld">添加数据的节点</param> public void TreeDataBind(DataView dv,TreeNode tnOld) { TreeNode tnNew; //创建一个新的节点 foreach (DataRowView drv in dv) { //为新的借点设置属性 tnNew = tnOld.Nodes.Add(drv["tname"].ToString()); tnNew.Tag = drv["tid"]; //过滤数据视图 父类id = 上一级的tid dv.RowFilter = "pid=" + drv["tid"].ToString(); //自己调用自己 TreeDataBind(dv, tnNew); } }
调用的方法很简单
DataTable dtRet = (DataTable)dh.ExecProcRetObj(ep); DataView dv = new DataView(dtRet); dv.RowFilter = "pid=0"; TreeDataBind(dv, this.treeView1.Nodes.Add("商品类别"));
效果
本文转载:http://blog.csdn.net/q107770540/article/details/7708418
List<DetptInfo> lstDept = new List<DetptInfo> { new DetptInfo {ID=1,DeptName="公司",ParentID=0}, new DetptInfo {ID=10,DeptName="软件中心",ParentID=1}, new DetptInfo {ID=11,DeptName="综合办公室",ParentID=1}, new DetptInfo {ID=100,DeptName="人力资源部",ParentID=11}, new DetptInfo {ID=101,DeptName="行政部",ParentID=11}, }; public static List<DetptInfo> GetSonID(List<DetptInfo> lstDept, int p_id) { var query = from c in lstDept where c.ParentID == p_id select c; return query.Concat(query.SelectMany(t => GetSonID(lstDept, t.ID))).ToList(); } --调用: var query = GetSonID(lstDept, 0); Console.WriteLine("Id\tName\tParent"); query.ToList().ForEach(q => Console.WriteLine("{0}\t{1}\t{2}", q.ID, q.DeptName, q.ParentID));