DevExpress TreeList 自动加载数据源并显示漂亮图标
DevExpress TreeList 自动加载数据源并显示漂亮图标
1. 首先设置TreeList.StateImageList属性,绑定imageList对象。
2. 加载数据表,必须是支持父子级结构的数据。
3. 设置结点Node.ImageIndex属性,使用递归方法SetImageIndex
扫一扫加作者微信
1. 首先设置TreeList.StateImageList属性,绑定imageList对象。
2. 加载数据表,必须是支持父子级结构的数据。
3. 设置结点Node.ImageIndex属性,使用递归方法SetImageIndex
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
扫一扫加作者微信
版权声明:本文为开发框架文库发布内容,转载请附上原文出处连接
NewDoc C/S框架网