C# TreeView与DataTable交互添加节点两种方式
程序员文章站
2022-06-08 14:15:29
...
本文转载:http://dengzebo.blog.163.com/blog/static/18867406201032141742168/
方式一:
#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 的 代码
with as 递归查询
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("商品类别"));
效果如下: