場景 Winform控制項-DevExpress18下載安裝註冊以及在VS中使用: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/100061243 DevExpress的TreeList怎樣設置數據源,從實例入手: https:/ ...
場景
Winform控制項-DevExpress18下載安裝註冊以及在VS中使用:
https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/100061243
DevExpress的TreeList怎樣設置數據源,從實例入手:
https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/102548490
在上面實現簡單的TreeList並設置數據源,顯示效果仍然不是想要的樹形結構的效果。
如果想實現如下這樣效果,還需要進行進一步的屬性設置。
註:
博客主頁:
https://blog.csdn.net/badao_liumang_qizhi
關註公眾號
霸道的程式猿
獲取編程相關電子書、教程推送與免費下載。
實現
/// 為樹控制項設置數據源 /// </summary> /// <param name="treeList">樹控制項</param> /// <param name="data">數據對象</param> /// <param name="fieldName">顯示的欄位</param> /// <param name="keyFieldName">鍵欄位</param> /// <param name="parentFieldName">父級鍵欄位</param> public static void SetTreeListDataSource(DevExpress.XtraTreeList.TreeList treeList, List<DataTreeNode> data, string fieldName, string keyFieldName, string parentFieldName) { #region 設置節點圖標 System.Windows.Forms.ImageList imgList = new System.Windows.Forms.ImageList(); imgList.Images.AddRange(imgs); treeList.SelectImageList = imgList; //目錄展開 treeList.AfterExpand -= treeList_AfterExpand; treeList.AfterExpand += treeList_AfterExpand; //目錄摺疊 treeList.AfterCollapse -= treeList_AfterCollapse; treeList.AfterCollapse += treeList_AfterCollapse; //數據節點單擊,開啟整行選中 treeList.MouseClick -= treeList_MouseClick; treeList.MouseClick += treeList_MouseClick; //數據節點雙擊選中 treeList.MouseDoubleClick -= treeList_MouseDoubleClick; treeList.MouseDoubleClick += treeList_MouseDoubleClick; //焦點離開事件 treeList.LostFocus -= treeList_LostFocus; treeList.LostFocus += treeList_LostFocus; #endregion #region 設置列頭、節點指示器面板、表格線樣式 treeList.OptionsView.ShowColumns = false; //隱藏列標頭 treeList.OptionsView.ShowIndicator = false; //隱藏節點指示器面板 treeList.OptionsView.ShowHorzLines = false; //隱藏水平表格線 treeList.OptionsView.ShowVertLines = false; //隱藏垂直表格線 treeList.OptionsView.ShowIndentAsRowStyle = false; #endregion #region 初始禁用單元格選中,禁用整行選中 treeList.OptionsView.ShowFocusedFrame = true; //設置顯示焦點框 treeList.OptionsSelection.EnableAppearanceFocusedCell = false; //禁用單元格選中 treeList.OptionsSelection.EnableAppearanceFocusedRow = false; //禁用正行選中 //treeList.Appearance.FocusedRow.BackColor = System.Drawing.Color.Red; //設置焦點行背景色 #endregion #region 設置TreeList的展開摺疊按鈕樣式和樹線樣式 treeList.OptionsView.ShowButtons = true; //顯示展開摺疊按鈕 treeList.LookAndFeel.UseDefaultLookAndFeel = false; //禁用預設外觀與感覺 treeList.LookAndFeel.UseWindowsXPTheme = true; //使用WindowsXP主題 treeList.TreeLineStyle = DevExpress.XtraTreeList.LineStyle.Percent50; //設置樹線的樣式 #endregion #region 添加單列 DevExpress.XtraTreeList.Columns.TreeListColumn colNode = new DevExpress.XtraTreeList.Columns.TreeListColumn(); colNode.Name = String.Format("col{0}", fieldName); colNode.Caption = fieldName; colNode.FieldName = fieldName; 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; //移除列後是否允許在自定義窗體中顯示 treeList.Columns.Clear(); treeList.Columns.AddRange(new DevExpress.XtraTreeList.Columns.TreeListColumn[] { colNode }); #endregion #region 綁定數據源 treeList.KeyFieldName = keyFieldName; treeList.ParentFieldName = parentFieldName; treeList.DataSource = data; treeList.RefreshDataSource(); #endregion #region 初始化圖標 SetNodeImageIndex(treeList.Nodes.FirstOrDefault()); #endregion }