修改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;
        }
时间: 2024-07-31 14:25:54

修改TreeList单元格格式(实现类似单元格合并效果)的相关文章

DataGridView导出数据到Excel及单元格格式的修改

在软件开发过程中,时常会遇到把一些数据信息从DataGridView中导出到Excel表格中的情况,如果写的多了就会发现挺简单的,我们不妨来写一写,留作备用,毕竟有时候Ctrl+C和Ctrl+V还是比较方便的. 思路很简单,写一个Module,然后调用: Module代码如下: <span style="font-family:Times New Roman;font-size:18px;">'****************************************

POI设置EXCEL单元格格式为文本、小数、百分比、货币、日期、科学计数法和中文大写

再读本篇文章之前,请先看我的前一篇文章,前一篇文章中有重点讲到POI设置EXCEL单元格格式为文本格式,剩下的设置小数.百分比.货币.日期.科学计数法和中文大写这些将在下面一一写出 以下将要介绍的每一种都会用到这三行中的变量 HSSFWorkbook demoWorkBook = new HSSFWorkbook(); HSSFSheet demoSheet = demoWorkBook.createSheet("The World's 500 Enterprises"); HSSFC

DataGridView导出数据到Excel及单元格格式的改动

在软件开发过程中,时常会遇到把一些数据信息从DataGridView中导出到Excel表格中的情况.假设写的多了就会发现挺简单的,我们最好还是来写一写,留作备用,毕竟有时候Ctrl+C和Ctrl+V还是比較方便的. 思路非常easy.写一个Module,然后调用: Module代码例如以下: <span style="font-family:Times New Roman;font-size:18px;">'*********************************

Excel更改单元格格式后无效

问题描述: 比如修改了数据的自定义显示格式(日期显示 yyyy"年"m"月",手机号分段000-0000-0000),应用后发现只有部分生效,或者都不生效,再检查发现格式确实已经设置好了. 解决方案: 这其实是Excel的格式显示问题,一般双击单元格后格式就会自动修正.对于大量数据,可以选择数据--分列--完成,可以自动刷新.

Excel开发学习笔记:文件选择控件、查找匹配项、单元格格式及数据有效性

遇到一个数据处理自动化的问题,于是打算开发一个基于excel的小工具.在业余时间一边自学一边实践,抽空把一些知识写下来以备今后参考,因为走的是盲人摸象的野路子,幼稚与错误请多包涵. 开发环境基于VSTO,具体配置:visual studio 2010,VB .Net,excel 2007,文档级别的定制程序. Private OpenFileDialog1 As New OpenFileDialog  Private Sub test()      OpenFileDialog1.Filter 

datagridview 单元格格式转换注意

datagridview 单元格内容进行比较时要注意正确写法要用强制转换,否则出错Convert.ToString(grd_order.SelectedRows[0].Cells[1].Value)== "" 或者写成grd_order.Rows[t].Cells[1].EditedFormattedValue.ToString().TrimEnd() != ""也可以 grd_order.Rows[t].Cells[1].value出错grd_order.Rows

C# 设置单元格格式属性

数字(Range.NumberFormatlocal 属性)常规:Range.NumberFormatlocal = "G/通用格式"数值:Range.NumberFormatlocal = "0.000_ " --保留小数位数为3            Range.NumberFormatlocal = "0" --不要小数            Range.NumberFormatlocal = "#,##0.000 "

POI中设置Excel单元格格式样式(居中,字体,边框等)

创建sheet什么的就不多说了,直接进入正题 HSSFCellStyle cellStyle = wb.createCellStyle(); 一.设置背景色: cellStyle.setFillForegroundColor((short) 13);// 设置背景色 cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); 二.设置边框: cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THI

Excel_单元格格式_查找替换、定位

不重复! 显示格式:Ctrl+1 1,合并后居中,填充颜色,设置单元格边框,划斜线,格式刷(单击,双击) 2,单元格数字格式,格式不会改变值!自定义(编码规则) 4个 a :只显示星期:周+aaa:周几: 文本:有特殊意义:文本转换成数字:点击感叹号 3,分列工具:日期.身份证等转化有问题可以用! 扩展:有一套编码规则 4,查找与替换:Ctrl+F 英文通配符!最好选中单元格匹配,尽量不省略 颜色,格式,文字都可以: *文字:表示含有以‘文字’结尾的字符串 文字?:?表示一个字符 文字~*:波浪