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"] = "1"; row["Name"] = "红色"; table.Rows.Add(row); row = table.NewRow(); row["parentId"] = "*"; row["Id"] = "2"; row["Name"] = "黄色"; table.Rows.Add(row); row = table.NewRow(); row["parentId"] = "*"; row["Id"] = "3"; row["Name"] = "绿色"; table.Rows.Add(row); row = table.NewRow(); row["parentId"] = "1"; row["Id"] = "01"; row["Name"] = "粉红色"; table.Rows.Add(row); row = table.NewRow(); row["parentId"] = "2"; row["Id"] = "02"; 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 = 0; 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 = 0; 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分了,如果需要完整代码的可以去下载一下。