DevExpress的TreeList怎样设置数据源使其显示成单列树形结构
程序员文章站
2022-06-29 08:52:19
场景 Winform控件-DevExpress18下载安装注册以及在VS中使用: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/100061243 在上面已经实现DevExpress的安装之后,拖拽一个TreeList,然后怎样给 ......
场景
winform控件-devexpress18下载安装注册以及在vs中使用:
https://blog.csdn.net/badao_liumang_qizhi/article/details/100061243
在上面已经实现devexpress的安装之后,拖拽一个treelist,然后怎样给其设置数据源。
devexpress的treelist怎样设置数据源,从实例入手:
https://blog.csdn.net/badao_liumang_qizhi/article/details/102548490
参照上面进行设置数据源的过程。
如果要实现这样单列的树形结构应该怎样设置参数以及数据源。
注:
博客主页:
关注公众号
霸道的程序猿
获取编程相关电子书、教程推送与免费下载。
实现
首先新建一个winform程序,然后拖拽一个treelist控件
然后双击进入窗体的加载事件中,如果在别的地方设置数据源同理。
首先进行treelist样式等的设置
#region 设置列头、节点指示器面板、表格线样式 treelist1.optionsview.showcolumns = false; //隐藏列标头 treelist1.optionsview.showindicator = false; //隐藏节点指示器面板 treelist1.optionsview.showhorzlines = false; //隐藏水平表格线 treelist1.optionsview.showvertlines = false; //隐藏垂直表格线 treelist1.optionsview.showindentasrowstyle = false; #endregion #region 初始禁用单元格选中,禁用整行选中 treelist1.optionsview.showfocusedframe = true; //设置显示焦点框 treelist1.optionsselection.enableappearancefocusedcell = false; //禁用单元格选中 treelist1.optionsselection.enableappearancefocusedrow = false; //禁用正行选中 #endregion #region 设置treelist的展开折叠按钮样式和树线样式 treelist1.optionsview.showbuttons = true; //显示展开折叠按钮 treelist1.lookandfeel.usedefaultlookandfeel = false; //禁用默认外观与感觉 treelist1.lookandfeel.usewindowsxptheme = true; //使用windowsxp主题 treelist1.treelinestyle = devexpress.xtratreelist.linestyle.percent50; //设置树线的样式 #endregion
然后设置其单列显示
#region 添加单列 devexpress.xtratreelist.columns.treelistcolumn colnode = new devexpress.xtratreelist.columns.treelistcolumn(); colnode.name = string.format("col{0}", "nodetext"); colnode.caption = "nodetext"; colnode.fieldname = "nodetext"; colnode.visibleindex = 0; colnode.visible = true; colnode.optionscolumn.allowedit = false; //是否允许编辑 colnode.optionscolumn.allowmove = false; //是否允许移动 colnode.optionscolumn.allowmovetocustomizationform = false; //是否允许移动至自定义窗体 colnode.optionscolumn.allowsort = false; //是否允许排序 colnode.optionscolumn.fixedwidth = false; //是否固定列宽 colnode.optionscolumn.readonly = true; //是否只读 colnode.optionscolumn.showincustomizationform = true; //移除列后是否允许在自定义窗体中显示 treelist1.columns.clear(); treelist1.columns.addrange(new devexpress.xtratreelist.columns.treelistcolumn[] { colnode }); #endregion
注:
这里的fieldname 要与数据源中要显示的属性名相对应。
然后设置作为识别节点的主键字段,这里使用id来区分标记每个节点;设置其父节点的标记属性。
treelist1.keyfieldname = "id"; treelist1.parentfieldname = "parentid";
为了构建数据源,我们需要新建一个datatreenode类
using system; using system.collections.generic; using system.linq; using system.text; using system.threading.tasks; namespace treelisttest { public class datatreenode { private string id; private string parentid; private string nodetext; public string id { get { return id; } set { id = value; } } public string parentid { get { return parentid; } set { parentid = value; } } public string nodetext { get { return nodetext; } set { nodetext = value; } } } }
然后再回到上面构建数据源
datatreenode node1 = new datatreenode(); node1.id = "1"; node1.parentid = null; node1.nodetext = "1公众号"; datatreenode node11 = new datatreenode(); node11.id = "2"; node11.parentid = "1"; node11.nodetext = "1-1霸道的程序猿"; datatreenode node111 = new datatreenode(); node111.id = "3"; node111.parentid = "2"; node111.nodetext = "1-1-1大量编程资源"; list<datatreenode> data = new list<datatreenode>(); data.add(node1); data.add(node11); data.add(node111);
然后给treelist设置数据源
treelist1.datasource = data; treelist1.refreshdatasource();
最终完整的窗体加载方法代码
private void form1_load(object sender, eventargs e) { #region 设置列头、节点指示器面板、表格线样式 treelist1.optionsview.showcolumns = false; //隐藏列标头 treelist1.optionsview.showindicator = false; //隐藏节点指示器面板 treelist1.optionsview.showhorzlines = false; //隐藏水平表格线 treelist1.optionsview.showvertlines = false; //隐藏垂直表格线 treelist1.optionsview.showindentasrowstyle = false; #endregion #region 初始禁用单元格选中,禁用整行选中 treelist1.optionsview.showfocusedframe = true; //设置显示焦点框 treelist1.optionsselection.enableappearancefocusedcell = false; //禁用单元格选中 treelist1.optionsselection.enableappearancefocusedrow = false; //禁用正行选中 #endregion #region 设置treelist的展开折叠按钮样式和树线样式 treelist1.optionsview.showbuttons = true; //显示展开折叠按钮 treelist1.lookandfeel.usedefaultlookandfeel = false; //禁用默认外观与感觉 treelist1.lookandfeel.usewindowsxptheme = true; //使用windowsxp主题 treelist1.treelinestyle = devexpress.xtratreelist.linestyle.percent50; //设置树线的样式 #endregion #region 添加单列 devexpress.xtratreelist.columns.treelistcolumn colnode = new devexpress.xtratreelist.columns.treelistcolumn(); colnode.name = string.format("col{0}", "nodetext"); colnode.caption = "nodetext"; colnode.fieldname = "nodetext"; colnode.visibleindex = 0; colnode.visible = true; colnode.optionscolumn.allowedit = false; //是否允许编辑 colnode.optionscolumn.allowmove = false; //是否允许移动 colnode.optionscolumn.allowmovetocustomizationform = false; //是否允许移动至自定义窗体 colnode.optionscolumn.allowsort = false; //是否允许排序 colnode.optionscolumn.fixedwidth = false; //是否固定列宽 colnode.optionscolumn.readonly = true; //是否只读 colnode.optionscolumn.showincustomizationform = true; //移除列后是否允许在自定义窗体中显示 treelist1.columns.clear(); treelist1.columns.addrange(new devexpress.xtratreelist.columns.treelistcolumn[] { colnode }); #endregion treelist1.keyfieldname = "id"; treelist1.parentfieldname = "parentid"; datatreenode node1 = new datatreenode(); node1.id = "1"; node1.parentid = null; node1.nodetext = "1公众号"; datatreenode node11 = new datatreenode(); node11.id = "2"; node11.parentid = "1"; node11.nodetext = "1-1霸道的程序猿"; datatreenode node111 = new datatreenode(); node111.id = "3"; node111.parentid = "2"; node111.nodetext = "1-1-1大量编程资源"; list<datatreenode> data = new list<datatreenode>(); data.add(node1); data.add(node11); data.add(node111); treelist1.datasource = data; treelist1.refreshdatasource(); }
上一篇: 产后黄花菜吃了对于孕妇身体好吗
下一篇: 网络推广相比于传统推广的优势