修改TreeList单元格格式(实现类似单元格合并效果)
关键点:(1)TreeList中显示的单元格默认不显示上、下、左、右边框,显示的是TreeList自身的行横边框、列纵边框,具体对应TreeList属性中OptionView项下的ShowVertLines、ShowHorzLines两项,将其对应默认值由默认False改为True即可去除行横边框、列纵边框,然后设置怎样的单元格格式显示什么样的单元格格式;
(2)在*_CustomDrawNodeCell中修改函数,而不是*_NodeCellStyle中修改,另外需要注意,前者在后者前运行,因此后者中的修改会覆盖掉前者中的修改。
(3)绘制TreeList单元格网线时,自动会覆盖掉单元格的所有边框,这与WinForm中只需绘制右、下,不需要绘制左上边框不同。
(4)如果只想汇总Treelist单元格边框,则不需要设置e.Hander = true;而如果想重绘单元格值或填充颜色则必须设置e.Hander = true;并且其值域不同,重绘的区域不同,即可以针对性重绘。
效果图如下:
举例:
(1)具体实现相关代码如下:
private void tlPublic_CustomDrawNodeCell(object sender, DevExpress.XtraTreeList.CustomDrawNodeCellEventArgs e)
{
DataRow dr = e.Node.Tag as DataRow;
if (dr != null)
{
Brush grid = new SolidBrush(Color.LightGray);//Color.Gray
Pen gridPen = new Pen(grid); if (dr["PublicId"].ToString() == "-2")
{
//e.Appearance.ForeColor = TrueLore.PBTool.BaseUI.DataGridViewComponent.GridViewSumRowFontColor;
e.Appearance.Font = new Font(this.Font, FontStyle.Bold);
}
else if (e.Node.HasChildren)
{
Color parentCellBackColor = TrueLore.PBTool.BaseUI.DataGridViewComponent.TreeGridViewBidFileRowBackColor;
e.Cache.FillRectangle(parentCellBackColor,e.Bounds);
////绘制值
e.Graphics.DrawLine(gridPen, e.Bounds.X, e.Bounds.Y, e.Bounds.X + e.Bounds.Width, e.Bounds.Y);
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);
}
else
{
#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.Node.NextNode == null)
{
//下边缘的线
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);
}
else if (e.Node.GetDisplayText(e.Column.AbsoluteIndex) != e.Node.NextNode.GetDisplayText(e.Column.AbsoluteIndex))
{
//下边缘的线
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);
}
else
{
e.Graphics.FillRectangle(cellBackBrush, e.Bounds);
e.Handled = true; //显示重绘(放置位置即值域不同,显示重绘的区域不同)
}
//首子节点绘制上边框,之所以放在代码最后是因为放在前面可能在单元格填充时覆盖掉
if (e.Node == e.Node.ParentNode.FirstNode)
{
e.Graphics.DrawLine(gridPen, e.Bounds.X, e.Bounds.Y, e.Bounds.X + e.Bounds.Width, e.Bounds.Y);
}
}
//非合并列
else
{
//当绘制TreeList单元格时会覆盖掉单元格的上下左右边框,因为TreeList的列纵线被设置为显示,行横线被设置为不显示,所以只需绘制上下线即可。
e.Graphics.DrawLine(gridPen, e.Bounds.X, e.Bounds.Y, e.Bounds.X + e.Bounds.Width, e.Bounds.Y);
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);
}
#endregion
}
} }
多加代码:
(2)具体实现相关代码如下:
//需要(行、列)合并的所有列标题名
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;
}
(3)不想覆盖已有单元格内容(选择性覆盖):首选将数节点的纵向边框去掉,再则添加事件及代码如下:
private void tlcBidEvalationPrice_CustomDrawNodeCell(object sender, DevExpress.XtraTreeList.CustomDrawNodeCellEventArgs e)
{
if (e.Node == null)
{
return;
} DataRow dr = e.Node.Tag as DataRow; if (dr != null)
{
Brush grid = new SolidBrush(Color.LightGray); //Color.Gray
Pen gridPen = new Pen(grid);
if (dr["PublicId"].ToString() == "-1")
{
Color parentCellBackColor = TrueLore.PBTool.BaseUI.DataGridViewComponent.TreeGridViewBidFileRowBackColor;
e.Cache.FillRectangle(parentCellBackColor, e.Bounds);
e.Graphics.DrawLine(gridPen, e.Bounds.X, e.Bounds.Y, e.Bounds.X + e.Bounds.Width, e.Bounds.Y); 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);
}
else if (dr["PublicId"].ToString() == "-2")
{
//Color sumRowBackColor = TrueLore.PBTool.BaseUI.DataGridViewComponent.GridViewSumRowBackColor;
//e.Cache.FillRectangle(sumRowBackColor, 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.DrawLine(gridPen, e.Bounds.X, e.Bounds.Y + e.Bounds.Height, e.Bounds.X + e.Bounds.Width, e.Bounds.Y + e.Bounds.Height);
}
else
{
#region TreeList实现类似单元格合并的效果
//现在属性中OptionView项下将ShowHorzlines设置为True
//当绘制TreeList单元格时会覆盖掉单元格的上下左右边框,因为TreeList的列纵线被设置为显示,行横线被设置为不显示,所以只需绘制上下线即可。 bool isMergeCell = false;
string cellCaption = string.Empty;
foreach (string caption in colsHeaderText_H)
{
if (this.tlcBidEvalationPrice.Columns[e.Column.AbsoluteIndex].Caption == caption)
{
isMergeCell = true;
cellCaption = caption;
break;
}
}
//纵向合并
if (isMergeCell == true)
{
Brush cellBackBrush = new System.Drawing.SolidBrush(e.Appearance.BackColor); //若与下一单元格值不同
if (e.Node.NextNode == null )//|| e.Node.NextNode.NextNode == null
{
e.Graphics.FillRectangle(cellBackBrush, 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.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.Black, e.Bounds.X, e.Bounds.Y, StringFormat.GenericTypographic);
}
else if (e.Node.GetDisplayText(e.Column.AbsoluteIndex) != e.Node.NextNode.GetDisplayText(e.Column.AbsoluteIndex))
{
e.Graphics.FillRectangle(cellBackBrush, e.Bounds);
}
else
{
e.Graphics.FillRectangle(cellBackBrush, e.Bounds);
}
}
//非合并列
else
{
//当绘制TreeList单元格时会覆盖掉单元格的上下左右边框,因为TreeList的列纵线被设置为显示,行横线被设置为不显示,所以只需绘制上下线即可。
e.Graphics.DrawLine(gridPen, e.Bounds.X, e.Bounds.Y, e.Bounds.X + e.Bounds.Width, e.Bounds.Y); //下边缘的线
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);
}
#endregion } //设置处理事件完成(关键点),只有设置为ture,才能显示出想要的结果。
//e.Handled = true; //不想覆盖已有内容就注掉(只绘制边框) } }
修改TreeList单元格格式(实现类似单元格合并效果)的更多相关文章
- excel将单元格格式由数字转为文本
由于python读取excel数据时,整数总会变成浮点数,例如1会变成1.0,这时可以通过将excel里面的数字转化为文本,再读取出来就不会变成浮点数了.还有一种情况是excel里面的数字是由公式计算 ...
- 王佩丰第2讲-excel单元格格式设置 笔记
点小箭头都可以进入单元格格式设置 跨越合并 添加斜线 回车 ALT+ENTER 格式刷 数字格式 特定红色 货币VS会计专用 日期 2是1月2号,3是1月3号-- 自定义[例子中是在数值后面加&quo ...
- 【JAVA】POI设置EXCEL单元格格式为文本、小数、百分比、货币、日期、科学计数法和中文大写
POI设置EXCEL单元格格式为文本.小数.百分比.货币.日期.科学计数法和中文大写 博客分类:,本文内容转自 http://javacrazyer.iteye.com/blog/894850 FIL ...
- POI设置EXCEL单元格格式为文本、小数、百分比、货币、日期、科学计数法和中文大写
再读本篇文章之前,请先看我的前一篇文章,前一篇文章中有重点讲到POI设置EXCEL单元格格式为文本格式,剩下的设置小数.百分比.货币.日期.科学计数法和中文大写这些将在下面一一写出 以下将要介绍的每一 ...
- Excel单元格格式设置
工作中遇到一些小问题: 例如办公自动化里的如何设置单元格格式 此格式分为两种:一种是样式上的格式 比如边框 行距字体等 第二种为数据格式: 比如每次我输入1000的话自动变红或者加粗字体 office ...
- Excel课程学习第二课单元格格式设置
今天要讲的是单元格格式的设置,字体字号的设置,边框设置,合并单元格之类的. 下面看看具体的内容: 1.使用单元格格式工具美化表格 1.1设置单元格格式的对话框在哪里? 下图中三个小箭头都能打开设置单元 ...
- DataGridView导出数据到Excel及单元格格式的改动
在软件开发过程中,时常会遇到把一些数据信息从DataGridView中导出到Excel表格中的情况.假设写的多了就会发现挺简单的,我们最好还是来写一写,留作备用,毕竟有时候Ctrl+C和Ctrl+V还 ...
- Python生成文本格式的excel\xlwt生成文本格式的excel\Python设置excel单元格格式为文本\Python excel xlwt 文本格式
Python生成文本格式的excel\xlwt生成文本格式的excel\Python设置excel单元格格式为文本\Python excel xlwt 文本格式 解决: xlwt 中设置单元格样式主要 ...
- Excel-转换单元格格式的函数或“方法”汇总
14.转换单元格格式的函数或"方法"汇总 =value(单元格) #转换为数值 =A1&"" #转换A1为文本 = ...
随机推荐
- [转]linux sort 命令详解
原文网址:http://www.cnblogs.com/51linux/archive/2012/05/23/2515299.html 1 sort的工作原理 sort将文件的每一行作为一个单位,相互 ...
- vim学习笔记(2)——vim配置
记录vim的配置,随时更新 MacVim 安装: homebrew,安装位置:/usr/local/Cellar brew linkapps macvim--将macvim.app加入到Applica ...
- ComboxEdit 重要属性
DisplayMember="ComboItemName" ValueMember="ComboItemCode"IsTextEditable="Tr ...
- 【python】如何去掉使用BeautifulSoup读取html出现的警告UserWarning: You provided Unicode markup but also provided a value for from_encoding
如果我们这样读取html页面 soup= BeautifulSoup(rsp.text,'html.parser',from_encoding='utf-8') # 粗体部分多余了 就会出现下面的警 ...
- .7z.001,.7z.002这样的文件如何解压
1 如图所示,压缩分卷没有显示关联的软件来打开,Winrar右击也无法解压 2 可以使用7-ZIP软件打开该文件,然后选择提取(相当于Winrar的解压),然后选择提取路径,默认是同一个文件夹,点击确 ...
- GIS专业书籍、文档、数据、网站、工具等干货
整理.分享一些个人整理的GIS专业书籍.文档.数据.网站.工具等.也希望大家将自己的心得也分享出来,一起交流,共同进步. 如果下载链接失效,请到这里去:地信网 一.原理应用类 GIS基础类 01.地理 ...
- Session 共享(Custom模式)By Memcached(原创)
1.web.config配置: <machineKey decryptionKey="FD69B2EB9A11E3063518F1932E314E4AA1577BF0B824F369& ...
- Struts2(四)Action一接收参数
一.属性接收参数并输出 导入struts2的包,导入需要的包 和struts.xml配置文件 <?xml version="1.0" encoding="UTF-8 ...
- ADS ARM 汇编和GNU ARM汇编
Linux/Unix内核源代码用的编译器是GCC,而GCC采用的是AT&T的汇编格式,这与ADS下使用的汇编格式是不同的. 两种汇编格式的部分对比如下: GNU ARM汇编 ADS ARM汇编 ...
- eclipse使用egit插件
本来想用myeclipse,奈何试过网上所列的常用方法,都无法成功安装egit插件.只得转到eclipse.话说eclipse不仅是免费的,启动也较myeclipse更为迅速,安装插件也非常顺利.使用 ...