首页 > 代码库 > 修改TreeList单元格格式(实现类似单元格合并效果)

修改TreeList单元格格式(实现类似单元格合并效果)

关键点:(1)TreeList中显示的单元格默认不显示上、下、左、右边框,显示的是TreeList自身的行横边框、列纵边框,具体对应TreeList属性中OptionView项下的ShowVertLines、ShowHorzLines两项,将其对应默认值由默认False改为True即可去除行横边框、列纵边框,然后设置怎样的单元格格式显示什么样的单元格格式;

    (2)在*_CustomDrawNodeCell中修改函数,而不是*_NodeCellStyle中修改,另外需要注意,前者在后者前运行,因此后者中的修改会覆盖掉前者中的修改。

    (3)绘制TreeList单元格网线时,自动会覆盖掉单元格的所有边框,这与WinForm中只需绘制右、下,不需要绘制左上边框不同。

具体实现相关代码如下:

        //需要(行、列)合并的所有列标题名        List<String> colsHeaderText_H = new List<String>();        private void InitFormatColumns()        {            colsHeaderText_H.Add("量化合计");            colsHeaderText_H.Add("投标报价");            colsHeaderText_H.Add("评标价");        }        private void tlPublic_CustomDrawNodeCell(object sender, DevExpress.XtraTreeList.CustomDrawNodeCellEventArgs e)        {            DataRow dr = e.Node.Tag as DataRow;            if (dr != null)            {                Brush gridBrush = new SolidBrush(Color.Pink);                Brush grid = new SolidBrush(Color.Gray);                Pen gridPen = new Pen(grid);                ////e.Graphics.FillRectangle(gridBrush, e.Bounds);                //e.Cache.DrawRectangle(gridPen, e.Bounds);                //下边缘的线                //e.Graphics.DrawLine(gridPen, e.Bounds.X, e.Bounds.Y + e.Bounds.Height, e.Bounds.X + e.Bounds.Width, e.Bounds.Y + e.Bounds.Height);                ////绘制值                //e.Graphics.DrawString(e.Node.GetDisplayText(e.Column.AbsoluteIndex), e.Appearance.Font,                //        Brushes.Crimson, e.Bounds.X + 2, e.Bounds.Y + 2, StringFormat.GenericDefault);                if (dr["PublicId"].ToString() == "-2")                {                    e.Appearance.ForeColor = TrueLore.PBTool.BaseUI.DataGridViewComponent.GridViewSumRowFontColor;                    e.Appearance.Font = new Font(this.Font, FontStyle.Bold);                    //e.Graphics.FillRectangle(gridBrush, e.Bounds);                    //e.Cache.DrawRectangle(gridPen,e.Bounds);                    //下边缘的线                    //e.Graphics.DrawLine(gridPen, e.Bounds.X, e.Bounds.Y + e.Bounds.Height, e.Bounds.X + e.Bounds.Width, e.Bounds.Y + e.Bounds.Height);                                        ////绘制值                    //e.Graphics.DrawString(e.Node.GetDisplayText(e.Column.AbsoluteIndex), e.Appearance.Font,                    //        Brushes.Crimson, e.Bounds.X + 2, e.Bounds.Y + 2, StringFormat.GenericDefault);                }                else if (e.Node.HasChildren)                {                    //e.Appearance.BackColor = TrueLore.PBTool.BaseUI.DataGridViewComponent.TreeGridViewParentRowBackColor;                    Color parentCellBackColor = TrueLore.PBTool.BaseUI.DataGridViewComponent.TreeGridViewParentRowBackColor;                    //e.Cache.DrawRectangle(gridPen, e.Bounds);                    e.Cache.FillRectangle(parentCellBackColor,e.Bounds);                    ////绘制值                    e.Graphics.DrawString(e.Node.GetDisplayText(e.Column.AbsoluteIndex), e.Appearance.Font,                            Brushes.Crimson, e.Bounds.X + 2, e.Bounds.Y + 2, StringFormat.GenericDefault);//下边缘的线                    e.Graphics.DrawLine(gridPen, e.Bounds.X, e.Bounds.Y + e.Bounds.Height, e.Bounds.X + e.Bounds.Width, e.Bounds.Y + e.Bounds.Height);                                        //e.Handled = true;                                    }                else                {                    #region 注释代码                    //string tmpCaption = e.Column.Caption;                    //if ("量化合计" == tmpCaption || "投标报价" == tmpCaption || "评标价" == tmpCaption)                    //{                    //    e.Appearance.BorderColor = e.Appearance.BackColor;                    //    e.Handled = true;                    //}                    //e.Node.TreeList.RefreshCell(e.Node, e.Column);                    //e.Cache.DrawRectangle();                    #endregion                    #region TreeList实现类似单元格合并的效果                    //现在属性中OptionView项下将ShowHorzlines设置为True                    //当绘制TreeList单元格时会覆盖掉单元格的上下左右边框,因为TreeList的列纵线被设置为显示,行横线被设置为不显示,所以只需绘制上下线即可。                    bool isMergeCell = false;                    string cellCaption = string.Empty;                    foreach (string caption in colsHeaderText_H)                    {                        if (this.tlPublic.Columns[e.Column.AbsoluteIndex].Caption == caption)                         {                            isMergeCell = true;                            cellCaption = caption;                            break;                        }                    }                        //纵向合并                    if (isMergeCell == true)   //this.tlPublic.Columns[e.Column.AbsoluteIndex].Caption == caption) //&& e.Node.NextNode != null                        {                            Brush cellBackBrush = new System.Drawing.SolidBrush(e.Appearance.BackColor);                            //若与下一单元格值不同                            //if (e.CellValue.ToString() != e.Node.NextNode.GetDisplayText(e.Column.AbsoluteIndex))                            if (e.Node.NextNode == null)                            {                                //e.Cache.DrawRectangle(gridPen, e.Bounds);                                e.Graphics.FillRectangle(cellBackBrush, e.Bounds);                                //e.Appearance.FillRectangle(e.Cache, e.Bounds);                                //下边缘的线                                e.Graphics.DrawLine(gridPen, e.Bounds.X, e.Bounds.Y + e.Bounds.Height, e.Bounds.X + e.Bounds.Width, e.Bounds.Y + e.Bounds.Height);                                //绘制值                                e.Graphics.DrawString(e.Node.GetDisplayText(e.Column.AbsoluteIndex), e.Appearance.Font,                                        Brushes.Crimson, e.Bounds.X + 2, e.Bounds.Y + 2, StringFormat.GenericDefault);                            }                            else if (e.Node.GetDisplayText(e.Column.AbsoluteIndex) != e.Node.NextNode.GetDisplayText(e.Column.AbsoluteIndex))                            {                                //e.Cache.DrawRectangle(gridPen, e.Bounds);                                e.Graphics.FillRectangle(cellBackBrush, e.Bounds);                                //e.Appearance.FillRectangle(e.Cache, e.Bounds);                                //下边缘的线                                //e.Graphics.DrawLine(gridPen, e.Bounds.Left, e.Bounds.Bottom - 1, e.Bounds.Right, e.Bounds.Bottom);                                //e.Graphics.DrawLine(gridPen, e.Bounds.X, e.Bounds.Y + e.Bounds.Height, e.Bounds.X + e.Bounds.Width, e.Bounds.Y + e.Bounds.Height);                                //绘制值                                e.Graphics.DrawString(e.Node.GetDisplayText(e.Column.AbsoluteIndex), e.Appearance.Font,                                        Brushes.Crimson, e.Bounds.X + 2, e.Bounds.Y + 2, StringFormat.GenericDefault);                            }                            else                            {                                //e.Graphics.DrawLine(gridPen, e.Bounds.X, e.Bounds.Y + e.Bounds.Height, e.Bounds.X + e.Bounds.Width, e.Bounds.Y + e.Bounds.Height);                                //e.Appearance.FillRectangle(e.Cache, e.Bounds);                                e.Graphics.FillRectangle(cellBackBrush, e.Bounds);                            }                            //break;                        }                        //非合并列                        else                        {                            //e.Cache.DrawRectangle(gridPen, e.Bounds);                             //当绘制TreeList单元格时会覆盖掉单元格的上下左右边框,因为TreeList的列纵线被设置为显示,行横线被设置为不显示,所以只需绘制上下线即可。                            e.Graphics.DrawLine(gridPen, e.Bounds.X, e.Bounds.Y, e.Bounds.X + e.Bounds.Width, e.Bounds.Y);                            //绘制值                            e.Graphics.DrawString(e.Node.GetDisplayText(e.Column.AbsoluteIndex), e.Appearance.Font,                                    Brushes.Crimson, e.Bounds.X + 2, e.Bounds.Y + 2, StringFormat.GenericDefault);                            //e.Handled = true;                            ////下边缘的线                            e.Graphics.DrawLine(gridPen, e.Bounds.X, e.Bounds.Y + e.Bounds.Height, e.Bounds.X + e.Bounds.Width, e.Bounds.Y + e.Bounds.Height);                                                   }                        ////设置处理事件完成(关键点),只有设置为ture,才能显示出想要的结果。                        //e.Handled = true;                    #endregion                    //}                }                                //设置处理事件完成(关键点),只有设置为ture,才能显示出想要的结果。                e.Handled = true;                                      }        }        #endregion        private void tlPublic_NodeCellStyle(object sender, DevExpress.XtraTreeList.GetCustomNodeCellStyleEventArgs e)        {            //string tmpCaption = e.Column.Caption;            //if ("量化合计" == tmpCaption || "投标报价" == tmpCaption || "评标价" == tmpCaption)            //{            //    e.Appearance.BorderColor = e.Appearance.BackColor;            //    e.Appearance.ForeColor = Color.Red;            //    e.Node.TreeList.OptionsView.ShowFocusedFrame = false;            //e.Column.AppearanceCell.Options.UseBorderColor = true;            //e.Column.AppearanceCell.BorderColor = Color.DeepPink;            //}            //e.Appearance.BackColor = TrueLore.PBTool.BaseUI.DataGridViewComponent.TreeGridViewParentRowBackColor;        }

 

修改TreeList单元格格式(实现类似单元格合并效果)