Introduction

There are a lot of methods in the Internet solving the problem of how to merge GridView rows if neighboring cells show equal values. My approach is not the first; however, I think, it is rather universal and very short - less than 20 lines of code.

The algorithm is simple: to bypass all the rows, starting from the second at the bottom, to the top. If a cell value is the same as a value in the previous (lower) row, then increase RowSpan and make the lower cell invisible, and so forth.

The code that merges the cells is very short:

Hide   Copy Code
public class GridDecorator
{
public static void MergeRows(GridView gridView)
{
for (int rowIndex = gridView.Rows.Count - 2; rowIndex >= 0; rowIndex--)
{
GridViewRow row = gridView.Rows[rowIndex];
GridViewRow previousRow = gridView.Rows[rowIndex + 1]; for (int i = 0; i < row.Cells.Count; i++)
{
if (row.Cells[i].Text == previousRow.Cells[i].Text)
{
row.Cells[i].RowSpan = previousRow.Cells[i].RowSpan < 2 ? 2 :
previousRow.Cells[i].RowSpan + 1;
previousRow.Cells[i].Visible = false;
}
}
}
}
}
 

The last action is to add an OnPreRender event handler for the GridView:

Hide   Copy Code
protected void gridView_PreRender(object sender, EventArgs e)
{
GridDecorator.MergeRows(gridView);
}
 

合并Gridview单元格的更多相关文章

  1. Dev GridControl,GridView 显示多行文本及合并相同单元格

    显示多行文本的方法 首先把gridcontrol的views的Optionsview里的RowAutoHeight设置为True 在In-place Editor Repository 里添加 Mem ...

  2. Dev控件GridView单元格绑定控件

    Dev控件GridView单元格绑定控件 //文本按钮 RepositoryItemButtonEdit btnFields = new RepositoryItemButtonEdit();//创建 ...

  3. 黄聪:phpexcel中文教程-设置表格字体颜色背景样式、数据格式、对齐方式、添加图片、批注、文字块、合并拆分单元格、单元格密码保护

    首先到phpexcel官网上下载最新的phpexcel类,下周解压缩一个classes文件夹,里面包含了PHPExcel.php和PHPExcel的文件夹,这个类文件和文件夹是我们需要的,把class ...

  4. phpexcel中文教程-设置表格字体颜色背景样式、数据格式、对齐方式、添加图片、批注、文字块、合并拆分单元格、单元格密码保护

    转:http://www.cnblogs.com/huangcong/p/3687665.html 首先到phpexcel官网上下载最新的phpexcel类,下周解压缩一个classes文件夹,里面包 ...

  5. JQuery合并表格单元格

    转:http://www.cnblogs.com/xuguoming/p/3412124.html JQuery合并表格单元格   一.需求 如果存在一个表格,想把其中某一列内容相同的部分合并单元格, ...

  6. 利用jxl读取excel合并的单元格的一个小样例

    工作中我们可能要把Excel文件的记录保存到数据库, 今天我用jxl读取Excel文件时遇到了合并格的问题,记录例如以下: 如Excel文件例如以下: watermark/2/text/aHR0cDo ...

  7. 修复Java使用POI合并Excel单元格后,边框不显示的问题

    使用Apache POI生成Excel文档时,当进行单元格合并操作后,被合并的单元格边框会消失,使用如下方式可以解决. 创建方法: public void setBorderStyle(int bor ...

  8. PHPExcel-设置表格字体颜色背景样式、数据格式、对齐方式、添加图片、批注、文字块、合并拆分单元格、单元格密码保护

    首先到phpexcel官网上下载最新的phpexcel类,下周解压缩一个classes文件夹,里面包含了PHPExcel.php和PHPExcel的文件夹,这个类文件和文件夹是我们需要的,把class ...

  9. jxl获取excel中的合并的单元格(主要是方法介绍)

    Range[] rangeCells = sheet.getMergedCells();// 返回sheet中合并的单元格数组 for (Range r : rangeCells) {//对数组遍历拿 ...

随机推荐

  1. hadoop下HDFS基本命令使用

    前提:启动hadoop 1. 查看hdfs下 " / " 的目录 hdfs dfs -ls / 2. 创建文件夹(在 " / " 创建hadoop文件夹) hd ...

  2. SSH—网上商城之商品图片文件上传

    前言 网上商城中的淘宝图片要显示在页面的前提是图片应该已经在数据库里面,那么怎么实现图片的上传功能呢,这就是今天要说的主题. 内容 需求: 商城后台需要添加图片文件,用来图片显示 解决方式: Stru ...

  3. luogu1632 点的移动

    其实只需要开三重循环 根据OI中的一个重要的原理 给定一个序列a,求一个数x使得\(\sum |a_i-x|\)最小,那么这个数是序列a的中位数 证明略 然后既然是中位数,一定是数列中的数,类比到这题 ...

  4. django 异常问题总结

    1.问题1 在继承的类中: 403错误: views 添加:from django.core.context_processors import csrf def TestUEditor(reques ...

  5. [USACO18JAN]Cow at Large G(树形DP)

    P4186 [USACO18JAN]Cow at Large G(树形DP) Luogu4186 设dp[i]表示i点需要放多少个农民.则有 \(if(near[i]-dep[i]<=dep[i ...

  6. js-eval运算符

    js中使用eval运算符需要注意—— eval()只有一个参数 传入的参数是字符串时,才会去解析执行:否则,将直接返回这个参数 作用域与调用它的变量作用域保持一致 返回字符串中最后一个表达式或语句的值 ...

  7. 利用canvas添加图片水印--直接上代码

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...

  8. Applese 的毒气炸弹(最小生成树)

    链接:https://ac.nowcoder.com/acm/contest/330/G 来源:牛客网 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/C++ 262144K,其他语言5242 ...

  9. POJ2528 Mayor's posters(线段树+离散化)

    题意 : 在墙上贴海报, n(n<=10000)个人依次贴海报,给出每张海报所贴的范围li,ri(1<=li<=ri<=10000000).求出最后还能看见多少张海报. 分析 ...

  10. 2015苏州大学ACM-ICPC集训队选拔赛(1) 1008

    猪猪过河 Time Limit : 3000/1000ms (Java/Other)   Memory Limit : 65535/32768K (Java/Other) Total Submissi ...