DataGridView合并单元格
昨天一个同事问我DataGridView单元格合并的问题,一开始按照我的设想是算出两个单元格的Rectangle,
然后直接使用e.Graphics.FillRectangle(backColorBrush, rectangle)从新填充下背景色,然后在绘制显示的字符,当然这种想法是行不通的。
下面是我写的一个单元格合并的方法,其实这种方法并不好,只是看上去是把单元格合并了,其实实际DataGridView的列结构是没有发生变化,而且这种重绘的方法并不能编辑,所以我还是建议如果遇到合并单元格的问题,最好还是不要用DataGridView
如有不懂得可以加(源码分享群 81582487)一起交流
代码部分:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsDemo
{
public partial class DataGridViewMergeCell : Form
{
public DataGridViewMergeCell()
{
InitializeComponent();
Init();
}
private void DataGridViewMergeCell_Load(object sender, EventArgs e)
{
int rowIndex = view.Rows.Add();
DataGridViewRow row = view.Rows[rowIndex];
row.Cells["value"].Value = "bbb";
row.Cells["name"].Value = "aaa";
row.Cells["price"].Value = "bbb";
row.Cells["num"].Value = "aaa";
rowIndex = view.Rows.Add();
row = view.Rows[rowIndex];
row.Cells["value"].Value = "bbb";
row.Cells["name"].Value = "aaa";
row.Cells["price"].Value = "bbb";
row.Cells["num"].Value = "aaa";
rowIndex = view.Rows.Add();
row = view.Rows[rowIndex];
row.Cells["value"].Value = "bbb";
row.Cells["name"].Value = "aaa";
row.Cells["price"].Value = "bbb";
row.Cells["num"].Value = "aaa";
rowIndex = view.Rows.Add();
row = view.Rows[rowIndex];
row.Cells["value"].Value = "bbb";
row.Cells["name"].Value = "aaa";
row.Cells["price"].Value = "bbb";
row.Cells["num"].Value = "aaa";
rowIndex = view.Rows.Add();
row = view.Rows[rowIndex];
row.Cells["value"].Value = "bbb";
row.Cells["name"].Value = "aaa";
row.Cells["price"].Value = "bbb";
row.Cells["num"].Value = "aaa";
}
private void Init() {
view.AllowUserToAddRows = false;
// view.ColumnHeadersVisible = false;
view.AutoGenerateColumns = false;
view.AutoSize = false;
//view.RowHeadersVisible = false;
//view.GridColor = System.Drawing.ColorTranslator.FromHtml("#F8F8FF");
view.ColumnHeadersHeight = 60;
view.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.None;
view.ColumnHeadersBorderStyle = DataGridViewHeaderBorderStyle.Single;
view.Columns.Add(new DataGridViewTextBoxColumn() { Name = "name", HeaderText = "主体", Width = 90, ReadOnly = true,Visible = true, AutoSizeMode = DataGridViewAutoSizeColumnMode.None });
view.Columns.Add(new DataGridViewTextBoxColumn() { Name = "value", HeaderText = "", Width = 80, ReadOnly = true, Visible = true, AutoSizeMode = DataGridViewAutoSizeColumnMode.None });
view.Columns.Add(new DataGridViewTextBoxColumn() { Name = "price", HeaderText = "主体", Width = 90, ReadOnly = true, Visible = true, AutoSizeMode = DataGridViewAutoSizeColumnMode.None });
view.Columns.Add(new DataGridViewTextBoxColumn() { Name = "num", HeaderText = "", Width = 80, ReadOnly = true, Visible = true, AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill });
view.CellPainting -= new DataGridViewCellPaintingEventHandler(view_CellPainting);
view.CellPainting += new DataGridViewCellPaintingEventHandler(view_CellPainting);
}
void view_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
if (e.RowIndex == 1 && e.ColumnIndex == 1)
{
Brush backColorBrush = new SolidBrush(e.CellStyle.BackColor);
e.Paint(e.CellBounds, DataGridViewPaintParts.All);
e.Graphics.FillRectangle(backColorBrush, e.CellBounds);
//绘制背景色(被选中状态下)
if (e.State == (DataGridViewElementStates.Displayed | DataGridViewElementStates.Selected | DataGridViewElementStates.Visible))
e.PaintBackground(e.CellBounds, false);
//分别绘制原文本和现在改变颜色的文本
Brush fontColor = new SolidBrush(e.CellStyle.ForeColor);
// e.Graphics.DrawString("", this.Font, fontColor, e.CellBounds, StringFormat.GenericDefault);
//绘制下边框线
Brush gridBrush = new SolidBrush(this.view.GridColor);
Pen pen = new Pen(gridBrush);
e.Graphics.DrawLine(pen, e.CellBounds.Left, e.CellBounds.Bottom - 1,
e.CellBounds.Right - 1, e.CellBounds.Bottom - 1);
DataGridViewCell cell = this.view.Rows[e.RowIndex].Cells[e.ColumnIndex];
cell.Value = "";
e.Handled = true;
}
if (e.RowIndex == 1 && e.ColumnIndex == 2)
{
Brush backColorBrush = new SolidBrush(e.CellStyle.BackColor);
e.Paint(e.CellBounds, DataGridViewPaintParts.All);
e.Graphics.FillRectangle(backColorBrush, e.CellBounds);
//绘制背景色(被选中状态下)
if (e.State == (DataGridViewElementStates.Displayed | DataGridViewElementStates.Selected | DataGridViewElementStates.Visible))
e.PaintBackground(e.CellBounds, true);
//分别绘制原文本和现在改变颜色的文本
Brush fontColor = new SolidBrush(e.CellStyle.ForeColor);
// e.Graphics.DrawString("", this.Font, fontColor, e.CellBounds, StringFormat.GenericDefault);
//绘制下边框线
Brush gridBrush = new SolidBrush(this.view.GridColor);
Pen pen = new Pen(gridBrush);
e.Graphics.DrawLine(pen, e.CellBounds.Left, e.CellBounds.Bottom - 1,
e.CellBounds.Right - 1, e.CellBounds.Bottom - 1);
//绘制右边框线
e.Graphics.DrawLine(pen, e.CellBounds.Right - 1,
e.CellBounds.Top, e.CellBounds.Right - 1,
e.CellBounds.Bottom - 1);
DataGridViewCell cell = this.view.Rows[e.RowIndex].Cells[e.ColumnIndex];
cell.Value = "";
cell.Tag = "ccccc";
Rectangle rectanle = e.CellBounds;
rectanle.X = rectanle.X -15;
rectanle.Y = rectanle.Y + 5;
//这里需要注意的是我没有用 e.Graphics 原因是这个只能在当前单元格绘制
//而我是在DataGridView上面建一个绘图Graphics对象这样就可以看上去在两个单元格里面绘制了
//这里你们也可以自己试一试
Graphics graphics = this.view.CreateGraphics();
//分别绘制原文本和现在改变颜色的文本
graphics.DrawString("cccc", this.Font, new SolidBrush(e.CellStyle.ForeColor), rectanle, StringFormat.GenericDefault);
e.Handled = true;
}
}
}
}
以下是我的建议,这种虽然看上去是把问题解决了,其实这样有很多缺点,一个是不能编辑,而且这个还有个bug就是点击左边的边框的时候会把左边的文字隐藏起来,我试了很多方法都没解决,这个应该是编辑的时候背景颜色给遮住了。
一下推荐一个可以合并单元格的第三方控件(DevExpress.XtraGrid.v7.3.dll),这个现在我也没有研究,不过后续我会研究的,也会在写篇文章的
DataGridView合并单元格的更多相关文章
- DataGridView合并单元格(多行多列合并)
一.点击在拖入的显示控件(TreeList)右上方的箭头,在Treelist任务中选择数据源,添加项目数据源,依次选择数据库.数据集,新建连接,浏览选择数据库(*.mdb),依次点击 下一步,选择“表 ...
- DataGridView合并单元格(一列或一行)
#region"合并单元格的测试(一列或一行)" // int?是搜索一种类型(可空类型),普通的int不能为null,而用int?,其值可以为null //private int ...
- Windows Forms DataGridView中合并单元格
Windows Forms DataGridView 没有提供合并单元格的功能,要实现合并单元格的功能就要在CellPainting事件中使用Graphics.DrawLine和 Graphics.D ...
- datagridview 纵向 横向 合并单元格
datagridview 单元格合并:纵向以及横向合并参考了csdn上不知哪位的代码,具体哪位找不到连接了. 纵向合并: /// <summary> /// 纵向合并,即合并数据项的值 / ...
- NPOI_winfrom导出Excel表格(一)(合并单元格、规定范围加外边框、存储路径弹框选择)
1.导出 private void btn_print_Click(object sender, EventArgs e) { DataTable dtNew = new DataTable(); d ...
- C# 获取Excel中的合并单元格
C# 获取Excel中的合并单元格 我们在制作表格时,有时经常需要合并及取消合并一些单元格.在取消合并单元格时需要逐个查找及取消,比较麻烦.这里分享一个简单的方法来识别Excel中的合并单元格,识别这 ...
- jquery操作表格 合并单元格
jquery操作table,合并单元格,合并相同的行 合并的方法 $("#tableid").mergeCell({ cols:[X,X] ///参数为要合并的列}) /** * ...
- NPOI操作EXCEL(五)——含合并单元格复杂表头的EXCEL解析
我们在第三篇文章中谈到了那些非常反人类的excel模板,博主为了养家糊口,也玩命做出了相应的解析方法... 我们先来看看第一类复杂表头: ...... 博主称这类excel模板为略复杂表头模板(蓝色部 ...
- poi获取合并单元格内的第一行第一列的值
当读取如图所示的excel时,显示为第1行 第1列 的内容是:合并单元格 其它在合并单元格区域内的单元格不显示 示例代码如下: import java.io.FileInputStream; impo ...
随机推荐
- AEAI BPM流程集成平台V3.0.2版本开源发布
本次开源发布的是AEAI BPMV3.0.2版流程平台,该版本是数通畅联首次正式对外发布的版本,产品现已开源并上传至开源社区http://www.oschina.net/p/aeai-bpm. 产品说 ...
- .NET初学者推荐课程 asp.net错误代码大全
错误 CS0001 编译器内部错误错误 CS0003 内存溢出错误 CS0004 提升为错误的警告错误 CS0005 编译器选项后应跟正确的参数错误 CS0006 找不到动态链接的元数据文件错误 CS ...
- sql server 2008还原数据库,出现缺少介质问题
我在sql server2008中备份数据库时,新增了一个自己建立的数据库,备份成功后,在去别的电脑总是还原数据 还原不了,最后在网上找到了解决方案
- 重新想象 Windows 8 Store Apps (48) - 多线程之其他辅助类: SpinWait, SpinLock, Volatile, SynchronizationContext, CoreDispatcher, ThreadLocal, ThreadStaticAttribute
[源码下载] 重新想象 Windows 8 Store Apps (48) - 多线程之其他辅助类: SpinWait, SpinLock, Volatile, SynchronizationCont ...
- 验证控件插图扩展控件ValidatorCalloutExtender(用于扩展验证控件)和TextBoxWatermarkExtender
<asp:ScriptManager ID="ScriptManager1" runat="server"> </asp:ScriptMan ...
- LGLSearchBar
平时我们都是用UITextFeild 来写搜索框, 最近有时间就自己重新封装了UISearchBar, 他可以自行修改里面的属性来达到我们使用的要求. 源代码下载地址:https://github.c ...
- CentOS7 Debian 8 安装VMware-tools
如在安装过程中碰到未找到gcc 或者 kernel headers的可按以下方案解决,适用任意版本 CentOS 7 1. Update the kernel: $ yum update kernel ...
- 【JavaEE】SSH+Spring Security+Spring oauth2整合及example
现在加最后一样,就是oauth2,现在很多网站都有对应的移动版本,那么移动端访问服务端的服务怎么控制权限,我知道的主要是两种方法,第一是模拟浏览器,访问服务的时候会生成session,之后在移动端缓存 ...
- python基础之基本算法和装饰器
1.冒泡排序 关于冒泡排序实现大小比较,大索引会向后移动,这次循环将最大数值直接移动至最后. li = [,,,,] ): ]: temp = li[i] li[i] = li[i + ] li[i ...
- jQuery源码分析-01总体架构
1. 总体架构 1.1自调用匿名函数 self-invoking anonymous function 打开jQuery源码,首先你会看到这样的代码结构: (function( window, und ...