DevExpress TreeList 自动加载数据源并显示漂亮图标
![](http://www.csframework.com/images/article-type5.gif)
![DevExpress TreeList 自动加载数据源并显示漂亮图标 DevExpress TreeList 自动加载数据源并显示漂亮图标](http://www.csframework.com/images/seo/SEO-devExpress%E5%BC%80%E5%8F%91%E6%A1%86%E6%9E%B6.jpg)
1. 首先设置TreeList.StateImageList属性,绑定imageList对象。
2. 加载数据表,必须是支持父子级结构的数据。
3. 设置结点Node.ImageIndex属性,使用递归方法SetImageIndex
![贴图图片](http://www.csframework.com/upload/image/tree1(1).png)
C# Code:
private void LoadTree()
{
//获取数据源
DataTable dt = DataDictCache.Cache.UserGroup;
//设置字段
tlCategory.KeyFieldName = "GroupCode";
tlCategory.ParentFieldName = "ParentGroupCode";
tlCategory.DataSource = dt;
//递归设置图标
DevTreeListView.SetImageIndex(tlCategory, null, 1, 0);
tlCategory.ExpandAll();
}
//来源:C/S框架网(www.csframework.com) QQ:1980854898
private void LoadTree()
{
//获取数据源
DataTable dt = DataDictCache.Cache.UserGroup;
//设置字段
tlCategory.KeyFieldName = "GroupCode";
tlCategory.ParentFieldName = "ParentGroupCode";
tlCategory.DataSource = dt;
//递归设置图标
DevTreeListView.SetImageIndex(tlCategory, null, 1, 0);
tlCategory.ExpandAll();
}
//来源:C/S框架网(www.csframework.com) QQ:1980854898
C# Code:
/// <summary>
/// 设置TreeList显示的图标
/// </summary>
/// <param name="tl">TreeList组件</param>
/// <param name="node">当前结点,从根结构递归时此值必须=null</param>
/// <param name="nodeIndex">根结点图标(无子结点)</param>
/// <param name="parentIndex">有子结点的图标</param>
public static void SetImageIndex(TreeList tl, TreeListNode node, int nodeIndex, int parentIndex)
{
if (node == null)
{
foreach (TreeListNode N in tl.Nodes)
SetImageIndex(tl, N, nodeIndex, parentIndex);
}
else
{
if (node.HasChildren || node.ParentNode == null)
{
//node.SelectImageIndex = parentIndex;
node.StateImageIndex = parentIndex;
node.ImageIndex = parentIndex;
}
else
{
//node.SelectImageIndex = nodeIndex;
node.StateImageIndex = nodeIndex;
node.ImageIndex = nodeIndex;
}
foreach (TreeListNode N in node.Nodes)
{
SetImageIndex(tl, N, nodeIndex, parentIndex);
}
}
}
//来源:C/S框架网(www.csframework.com) QQ:1980854898
/// <summary>
/// 设置TreeList显示的图标
/// </summary>
/// <param name="tl">TreeList组件</param>
/// <param name="node">当前结点,从根结构递归时此值必须=null</param>
/// <param name="nodeIndex">根结点图标(无子结点)</param>
/// <param name="parentIndex">有子结点的图标</param>
public static void SetImageIndex(TreeList tl, TreeListNode node, int nodeIndex, int parentIndex)
{
if (node == null)
{
foreach (TreeListNode N in tl.Nodes)
SetImageIndex(tl, N, nodeIndex, parentIndex);
}
else
{
if (node.HasChildren || node.ParentNode == null)
{
//node.SelectImageIndex = parentIndex;
node.StateImageIndex = parentIndex;
node.ImageIndex = parentIndex;
}
else
{
//node.SelectImageIndex = nodeIndex;
node.StateImageIndex = nodeIndex;
node.ImageIndex = nodeIndex;
}
foreach (TreeListNode N in node.Nodes)
{
SetImageIndex(tl, N, nodeIndex, parentIndex);
}
}
}
//来源:C/S框架网(www.csframework.com) QQ:1980854898
![C/S框架网|原创精神.创造价值.打造精品](http://www.csframework.com/images/our03.gif)
扫一扫加作者微信
![C/S框架网作者微信](http://www.csframework.com/images/wechatcode.jpg)
![C/S框架网|原创作品.质量保障.竭诚为您服务](http://www.csframework.com/images/CS框架网提供高效优质服务.jpg)
版权声明:本文为开发框架文库发布内容,转载请附上原文出处连接
NewDoc C/S框架网