Devexpress WinForm TreeList的三种数据绑定方式(DataSource绑定、AppendNode添加节点、VirtualTreeGetChildNodes(虚拟树加载模式))
第一种:DataSource绑定,这种绑定方式需要设置TreeList的ParentFieldName和KeyFieldName两个属性,这里需要注意的是KeyFieldName的值必须是唯一的。
代码如下:
private void Form1_Load(object sender, EventArgs e)
{
try
{
//构建一个DataTable数据源
DataTable table = new DataTable();
table.Columns.Add("parentId");
table.Columns.Add("Id");
table.Columns.Add("parentName");
table.Columns.Add("Name");
DataRow row = table.NewRow();
row["parentId"] = "";
row["Id"] = "*";
row["Name"] = "所有颜色";
table.Rows.Add(row);
row = table.NewRow();
row["parentId"] = "*";
row["Id"] = "";
row["Name"] = "红色";
table.Rows.Add(row);
row = table.NewRow();
row["parentId"] = "*";
row["Id"] = "";
row["Name"] = "黄色";
table.Rows.Add(row);
row = table.NewRow();
row["parentId"] = "*";
row["Id"] = "";
row["Name"] = "绿色";
table.Rows.Add(row);
row = table.NewRow();
row["parentId"] = "";
row["Id"] = "";
row["Name"] = "粉红色";
table.Rows.Add(row);
row = table.NewRow();
row["parentId"] = "";
row["Id"] = "";
row["Name"] = "鹅黄色";
table.Rows.Add(row);
treeList1.ParentFieldName = "parentId";
treeList1.KeyFieldName = "Id";
treeList1.DataSource = table;
treeList1.ExpandAll();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
} private void treeList1_BeforeCheckNode(object sender, DevExpress.XtraTreeList.CheckNodeEventArgs e)
{
e.State = (e.PrevState == CheckState.Checked ? CheckState.Unchecked : CheckState.Checked);
} private void treeList1_AfterCheckNode(object sender, DevExpress.XtraTreeList.NodeEventArgs e)
{
try
{
SetCheckedChildNodes(e.Node, e.Node.CheckState);
SetCheckedParentNodes(e.Node, e.Node.CheckState);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
} /// <summary>
/// 设置子节点的状态
/// </summary>
/// <param name="node"></param>
/// <param name="check"></param>
private void SetCheckedChildNodes(TreeListNode node, CheckState check)
{
for (int i = ; i < node.Nodes.Count; i++)
{
node.Nodes[i].CheckState = check;
SetCheckedChildNodes(node.Nodes[i], check);
}
} /// <summary>
/// 设置父节点的状态
/// </summary>
/// <param name="node"></param>
/// <param name="check"></param>
private void SetCheckedParentNodes(TreeListNode node, CheckState check)
{
if (node.ParentNode != null)
{
bool b = false;
CheckState state;
for (int i = ; i < node.ParentNode.Nodes.Count; i++)
{
state = (CheckState)node.ParentNode.Nodes[i].CheckState;
if (!check.Equals(state))
{
b = !b;
break;
}
}
node.ParentNode.CheckState = b ? CheckState.Checked : check;
SetCheckedParentNodes(node.ParentNode, check);
}
}
运行效果图:

第二种:AppendNode添加节点
代码如下:
private void LoadTree()
{
//清空节点
treeList1.BeginUnboundLoad();
treeList1.ClearNodes();
arrayList = new List<TreeListNode>();
//绑定部位树树的第一层
TreeListNode Node = treeList1.Nodes.Add(new object[] { "所有颜色", "*"});
Node.Tag = "*";
arrayList.Add(Node);
//绑定第二层
DataSet ds = SqlHelper.Query(@"select ctc.colorId as ParentNodeID,ctc.childcolorId as NodeID, cc.names as NodeName
from C_colorTocolor ctc(nolock)
inner join C_color cc(nolock) on ctc.childcolorId=cc.id");
if (ds != null && ds.Tables.Count > 0)
{
table = ds.Tables[0];
DataRow[] rows = table.Select("ParentNodeID='*'");
foreach (DataRow row in rows)
{
TreeListNode tn = treeList1.AppendNode(new object[] { row["NodeName"].ToString(), row["NodeID"].ToString() }, Node.Id);
tn.Tag = row["NodeID"].ToString();
arrayList.Add(tn);
BindNode(row["NodeID"].ToString(), tn);
}
}
treeList1.EndUnboundLoad();
treeList1.ExpandAll();
} private void BindNode(string nodeId, TreeListNode tn)
{
DataRow[] rows = table.Select("ParentNodeID='" + nodeId + "'");
foreach (DataRow row in rows)
{
TreeListNode tns = treeList1.AppendNode(new object[] { row["NodeName"].ToString(), row["NodeID"].ToString()}, tn.Id);
tns.Tag = row["NodeID"].ToString();
arrayList.Add(tns);
BindNode(row["NodeID"].ToString(), tns);
}
} private void Form2_Load(object sender, EventArgs e)
{
//绑定树
LoadTree();
}
运行效果图:

第三种:VirtualTreeGetChildNodes虚拟树加载。这种模式主要用于数据量过多,打开界面比较慢的情况。
private void Form3_Load(object sender, EventArgs e)
{
//初始化树
InitData();
} bool isload = false;
void InitData()
{
isload = false;
treeList1.ClearNodes();
treeList1.DataSource = new object();
} private void treeList1_VirtualTreeGetChildNodes(object sender, DevExpress.XtraTreeList.VirtualTreeGetChildNodesInfo e)
{
Cursor current = Cursor.Current;
Cursor.Current = Cursors.WaitCursor;
if (!isload)
{
List<O_node> roots = new List<O_node> { new O_node() { id= "*", names= "所有颜色",parentid="" } };
e.Children = roots;
isload = true;
}
else
{
try
{
O_node on = e.Node as O_node;
if(on!=null)
{
string sql = string.Format(@"select ctc.colorId as parentid,ctc.childcolorId as id, cc.names as names
from C_colorTocolor ctc(nolock)
inner join C_color cc(nolock) on ctc.childcolorId = cc.id
where ctc.colorId = '{0}'", on.id);
DataSet ds = SqlHelper.Query(sql);
if (ds != null && ds.Tables.Count > 0)
{
List<O_node> lst = (List<O_node>)DataTableToList<O_node>(ds.Tables[0]);
e.Children = lst;
}
else
{
e.Children = new object[] { };
}
}
else
{
e.Children = new object[] { };
}
}
catch
{
e.Children = new object[] { };
}
}
Cursor.Current = current;
} private void treeList1_VirtualTreeGetCellValue(object sender, DevExpress.XtraTreeList.VirtualTreeGetCellValueInfo e)
{
string name = string.Empty;
if (e.Node is O_node)
{
name = (e.Node as O_node).names;
}
if (e.Column == treeListColumn1) e.CellData = name;
} public static IList<T> DataTableToList<T>(DataTable table)
where T : class
{
if (!IsHaveRows(table))
return new List<T>();
IList<T> list = new List<T>();
T model = default(T);
foreach (DataRow dr in table.Rows)
{
model = Activator.CreateInstance<T>();
foreach (DataColumn dc in dr.Table.Columns)
{
object drValue = dr[dc.ColumnName];
PropertyInfo pi = model.GetType().GetProperty(dc.ColumnName);
if (pi != null && pi.CanWrite && (drValue != null && !Convert.IsDBNull(drValue)))
{
pi.SetValue(model, drValue, null);
}
}
list.Add(model);
}
return list;
} /// <summary>
/// 检查DataTable 是否有数据行
/// </summary>
/// <param name="dt">DataTable</param>
/// <returns></returns>
public static bool IsHaveRows(DataTable dt)
{
if (dt != null && dt.Rows.Count > 0)
{
return true;
}
else
{
return false;
}
}
} #region 节点
public class O_node
{
public string id { get; set; }
public string names { get; set; }
public string parentid { get; set; }
}
#endregion
运行效果图:


完整示例代码:https://download.csdn.net/download/u012026245/11986777
csdn现在资源积分上传时都是固定积分为5分了,如果需要完整代码的可以去下载一下。
Devexpress WinForm TreeList的三种数据绑定方式(DataSource绑定、AppendNode添加节点、VirtualTreeGetChildNodes(虚拟树加载模式))的更多相关文章
- ListView 的三种数据绑定方式
ListView 的三种数据绑定方式 1.最原始的绑定方式: public ObservableCollection<object> ObservableObj; public Mai ...
- C#Xml的三种创建方式(或者是两种?)和增删改查
一.Xml的创建方式 Xmlwriter(流式读取,Stream) 写过了:https://www.cnblogs.com/dengzhekaihua/p/15438493.html 这种方法虽然快, ...
- C# 三种打印方式含代码
一:C#代码直接打印pdf文件(打印质保书pdf文件) 引用: 代码注释很详细了. private void btn_pdf_Click(object sender, RoutedEventArgs ...
- 【转】vue.js三种安装方式
Vue.js(读音 /vjuː/, 类似于 view)是一个构建数据驱动的 web 界面的渐进式框架.Vue.js 的目标是通过尽可能简单的 API 实现响应的数据绑定和组合的视图组件.它不仅易于上手 ...
- vue.js三种安装方式
Vue.js(读音 /vjuː/, 类似于 view)是一个构建数据驱动的 web 界面的渐进式框架.Vue.js 的目标是通过尽可能简单的 API 实现响应的数据绑定和组合的视图组件.它不仅易于上手 ...
- 通过三个DEMO学会SignalR的三种实现方式
一.理解SignalR ASP .NET SignalR 是一个ASP .NET 下的类库,可以在ASP .NET 的Web项目中实现实时通信(即:客户端(Web页面)和服务器端可以互相实时的通知消息 ...
- Hive metastore三种配置方式
http://blog.csdn.net/reesun/article/details/8556078 Hive的meta数据支持以下三种存储方式,其中两种属于本地存储,一种为远端存储.远端存储比较适 ...
- django 模板语法和三种返回方式
模板 for循环 {% for athlete in athlete_list %} <li>{{ athlete.name }}</li> {% endfor %} if语句 ...
- js的三种继承方式及其优缺点
[转] 第一种,prototype的方式: //父类 function person(){ this.hair = 'black'; this.eye = 'black'; this.skin = ' ...
随机推荐
- csp-s模拟95
近两场开始文件评测,感觉和平时真不太一样. 考试时间是多了$10min$,但处理文件.输入输出什么的杂事也是忙来忙去.这可能也最像真实的$csps$了,稍有疏忽可能都是致命伤. T1.T2挂的对拍全打 ...
- php mvc 模式的开发注意事项
1.控制器中: 如果不涉及到数据库的就在控制器中. empty($res['code']) ? $this->error($res['msg']) : $this->success($re ...
- 以太坊geth区块链私链建立
以太坊geth区块链私链建立 geth的github https://github.com/ethereum/go-ethereum 下载最新(1.8)的geth,windows下安装很简单 关于 ...
- ailoop2里面的1个待考察的,在ailoop3里面的操作。(先使用海巨人,不使用英雄技能召唤图腾的问题)
承接上一篇博客HearthBuddy Ai 调试实战2 在使用海巨人的时候,少召唤了一个图腾(费用是对的) 研究ailoop2里面4个待考察的,在ailoop3里面的后续操作.ailoop3一共有36 ...
- ajax调用c#后端,发现参数没数值
之前是int的数据,名字是id 后面被改成字符串的数据,名字是encrptedId 因为名字不匹配,导致找不到数值.只需要把js里调用传递的参数名字改一下,或者C#后端,继续保持原来的名字
- win系统动态载入DLL所需要的三个函数详解(LoadLibrary,GetProcAddress,FreeLibrary)
动态载入 DLL 动态载入方式是指在编译之前并不知道将会调用哪些 DLL 函数, 完全是在运行过程中根据需要决定应调用哪些函数. 方法是:用 LoadLibrary 函数加载动态链接库到内存,用 Ge ...
- VUE数组操作方法的局限
1.不能通过索引值直接设置一个项: vm.items[indexOfItem] = newValue 但是可以用set方法设置: Vue.set(example1.items,indexOfItem, ...
- CDH集群部署hive建表中文乱码
背景:部署CDH集群的 hive 服务,选用 mysql 作为 hive 元数据的存储数据库,通过 hive cli 建表时发现中文注释均乱码. 现象:hive端建表中文注释乱码. 定位: 已经确认过 ...
- CockroachDB学习笔记——[译]为什么Go语言是CockroachDB的正确选择
原文链接:https://www.cockroachlabs.com/blog/why-go-was-the-right-choice-for-cockroachdb/ 原作者:Jessica Edw ...
- 启动页面、icon图标设置
更多尺寸像素如何放置请看:http://chicun.jammy.cc/ 如何设置App的启动图,也就是Launch Image? 新建一个iosLaunchImage文件夹