自定义TreeList单元格 z

DevExpress Treelist自定义单元格,加注释和行序号。以上一节的列表为例,实现以下效果:预算大于110万的单元格突出显示,加上行序号以及注释,如下图:

DevExpress Treelist 自定义单元格

添加行序号要用到CustomDrawNodeIndicator方法,要注意的是,取得的节点索引是从0开始的,所以要+1以便第一行从一开始算起。

  1. private void treeList1_CustomDrawNodeIndicator(object sender, CustomDrawNodeIndicatorEventArgs e)  
  2.         {  
  3.             TreeList tree = sender as DevExpress.XtraTreeList.TreeList;  
  4.             tree.IndicatorWidth = 30;  
  5.             IndicatorObjectInfoArgs args = e.ObjectArgs as IndicatorObjectInfoArgs;  
  6.             args.DisplayText = (tree.GetVisibleIndexByNode(e.Node) + 1).ToString();  
  7.         }  

若要添加单元格注释,则要用到toolTipController控件。将其拉入界面中,并将Treelist的 tooltipcontroller属性设置为刚才的控件名称,然后定义控件的GetActiveObjectInfo事件,本例显示了单元格的内容、列 和节点的索引:

  1. private void toolTipController1_GetActiveObjectInfo(object sender, DevExpress.Utils.ToolTipControllerGetActiveObjectInfoEventArgs e)  
  2.         {  
  3.             if (e.SelectedControl is DevExpress.XtraTreeList.TreeList)  
  4.             {  
  5.                 TreeList tree = (TreeList)e.SelectedControl;  
  6.                 TreeListHitInfo hit = tree.CalcHitInfo(e.ControlMousePosition);  
  7.   
  8.                 if (hit.HitInfoType == HitInfoType.Cell)  
  9.                 {  
  10.                     object cellInfo = new TreeListCellToolTipInfo(hit.Node,hit.Column,null);  
  11.                     string toolTip = string.Format("{0} (Colomn: {1}, Node ID: {2})", hit.Node[hit.Column],  
  12.                         hit.Column.VisibleIndex, hit.Node.Id);  
  13.                     e.Info = new DevExpress.Utils.ToolTipControlInfo(cellInfo,toolTip);  
  14.                 }  
  15.             }  
  16.         }  

最后,说一下自定义单元格,就是把符合条件的单元格按照定义的方式进行显示,例如本例是将预算大于110万的单元格背景变成粉色并且字体白色加粗显示。

  1. private void treeList1_NodeCellStyle(object sender, GetCustomNodeCellStyleEventArgs e)  
  2.         {  
  3.             if (e.Column.FieldName != "Budget") return;  
  4.             if (Convert.ToInt32(e.Node.GetValue(e.Column.AbsoluteIndex)) > 1100000)  
  5.             {  
  6.                 e.Appearance.BackColor = Color.FromArgb(80,255,0,255);  
  7.                 e.Appearance.ForeColor = Color.White;  
  8.                 e.Appearance.Font = new Font(e.Appearance.Font, FontStyle.Bold);  
  9.             }  
  10.         }  
原文地址:https://www.cnblogs.com/zeroone/p/3970111.html