DataGridView 合并同一列中值相同的相邻单元格 合并单元格
来源:互联网 发布:剑三小和尚的数据 编辑:程序博客网 时间:2024/06/10 12:21
/// <summary> /// 合并同一列中值相同的相邻单元格 /// </summary> /// <param name="dgv">DataGridView</param> /// <param name="columnIndexList">要合并的列的索引列表</param> /// <param name="e">当前单元格的属性访问器</param> private void MergeCellInOneColumn(DataGridView dgv, List<int> columnIndexList, DataGridViewCellPaintingEventArgs e) { if (columnIndexList.Contains(e.ColumnIndex) && e.RowIndex != -1) { Brush gridBrush = new SolidBrush(dgv.GridColor); Brush backBrush = new SolidBrush(e.CellStyle.BackColor); Pen gridLinePen = new Pen(gridBrush); //擦除 e.Graphics.FillRectangle(backBrush, e.CellBounds); //画右边线 e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right - 1, e.CellBounds.Top, e.CellBounds.Right - 1, e.CellBounds.Bottom - 1); //画底边线 if ((e.RowIndex < dgv.Rows.Count - 1 && dgv.Rows[e.RowIndex + 1].Cells[e.ColumnIndex].Value.ToString() != e.Value.ToString()) || e.RowIndex == dgv.Rows.Count - 1) { e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left, e.CellBounds.Bottom - 1, e.CellBounds.Right - 1, e.CellBounds.Bottom - 1); } //写文本 if (e.RowIndex == 0 || dgv.Rows[e.RowIndex - 1].Cells[e.ColumnIndex].Value.ToString() != e.Value.ToString()) { e.Graphics.DrawString((String)e.Value, e.CellStyle.Font, Brushes.Black, e.CellBounds.X + 2, e.CellBounds.Y + 5, StringFormat.GenericDefault); } e.Handled = true; } }
在写文本的位置(string)e.value 时会报错误,改写为e.value.tostring()才通过了。
接下来是写调用
private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e) { List<int> indexs = new List<int>() { 0, 1 }; MergeCellInOneColumn(dataGridView1, indexs, e); }
本文摘自:http://www.cnblogs.com/bloodofhero/archive/2012/07/05/2578068.html
- DataGridView 合并同一列中值相同的相邻单元格 合并单元格
- XtraGrid同一列中值相同的单元格合并
- jquery合并单元格(表格相邻列内容相同合并)
- gridview合并同一列相同内容的单元格
- 收录一个合并grivView合并同一列中的相同单元格
- table列的相同单元格合并
- excel合并相同列数据到同一单元格
- 扩展GridView控件(11) - 合并指定列的相邻且内容相同的单元格
- 扩展GridView控件(11) - 合并指定列的相邻且内容相同的单元格
- 用jQuery合并表格中左侧表头同列相同文本的相邻单元格
- 用jQuery合并表格中左侧表头同列相同文本的相邻单元格
- jquery 合并相邻且内容相同单元格
- 用jQuery合并表格中相同文本的相邻单元格
- 用jQuery合并表格中相同文本的相邻单元格
- 用Javascript合并指定表格相同文本的相邻单元格
- excel将某列内容相同的相邻单元格合并
- 用jQuery合并表格中相同文本的相邻单元格
- 用jQuery合并表格中相同文本的相邻单元格
- asp.net 操作带输入输出参数的分页存储过程(二)
- tolua++与c++交互
- php页面远程执行shell脚本
- MPI-IO:Open/close/delete
- GBin1专题之Web热点秀#12
- DataGridView 合并同一列中值相同的相邻单元格 合并单元格
- 《壹百度》- 百度员工内部培训资料
- Android环境不识别SDK(sdk版本过低)
- xshell 修复 nssock2.dll
- 使用encache
- pku 1129 Channel Allocation dfs 涂色问题
- Select For Update行级锁定
- 一个人心里的痛,没人懂,只是被自己极力的掩饰着
- 设计模式之十三------Template(模板)