惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

N
Netflix TechBlog - Medium
V
Vulnerabilities – Threatpost
Google Online Security Blog
Google Online Security Blog
Hugging Face - Blog
Hugging Face - Blog
L
LINUX DO - 热门话题
云风的 BLOG
云风的 BLOG
P
Proofpoint News Feed
D
Docker
C
Cyber Attacks, Cyber Crime and Cyber Security
MyScale Blog
MyScale Blog
P
Palo Alto Networks Blog
T
Tenable Blog
P
Privacy International News Feed
Google DeepMind News
Google DeepMind News
小众软件
小众软件
Cisco Talos Blog
Cisco Talos Blog
aimingoo的专栏
aimingoo的专栏
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
A
Arctic Wolf
C
Cybersecurity and Infrastructure Security Agency CISA
C
Cisco Blogs
T
Threat Research - Cisco Blogs
NISL@THU
NISL@THU
The Hacker News
The Hacker News
Project Zero
Project Zero
AWS News Blog
AWS News Blog
Simon Willison's Weblog
Simon Willison's Weblog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
T
Threatpost
V
Visual Studio Blog
The GitHub Blog
The GitHub Blog
The Cloudflare Blog
Last Week in AI
Last Week in AI
Jina AI
Jina AI
Cyberwarzone
Cyberwarzone
The Register - Security
The Register - Security
C
CXSECURITY Database RSS Feed - CXSecurity.com
Vercel News
Vercel News
D
Darknet – Hacking Tools, Hacker News & Cyber Security
MongoDB | Blog
MongoDB | Blog
U
Unit 42
Scott Helme
Scott Helme
A
About on SuperTechFans
WordPress大学
WordPress大学
F
Fortinet All Blogs
大猫的无限游戏
大猫的无限游戏
G
GRAHAM CLULEY
Latest news
Latest news
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
S
Schneier on Security

博客园 - 老D

.NET Memcached Client 扩展获取所有缓存Key SQL Server 2005 中新CTE语法 递归性能测试 在Crystal Report中将数字转为英文 连接远程服务器共享 在代码中恢复sql server 数据库 - 老D 获取同一网段内的SQL SERVER实例 C#动态加载DLL Asp.net 文件下载 在网页处理按键事件 - 老D - 博客园 SQL语句导入导出大全 跨应用程序进行 Forms 身份验证 GridView导出Excel ASP.NET数据库连接字符串的加密与解密 ASP.NET中GridView动态绑定数据实现编辑更新 ASp.NET 2.0中Page事件的执行顺序 批量insert数据 简繁转换 ASP.NET应用程序开发 经典算法-C#四种排序算法
合并 GridView 的单元格
老D · 2008-09-03 · via 博客园 - 老D

     在用GridView展示数据的时候需要把相同的列的单元格横或坚合并在一起(如下图所示),以前做这样的图一直用Repeater,但Repeater分页没有用GridView快,直接点分页就可以自动完成(人懒),现在换成用GridView做,没想到更简单

实现代码:

//在RowCreated事件中添加多一个表头列

   protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.Header)
        {

           //删除默认的2个cell,以便下面自己添加的列跟竖跨本默认列
            e.Row.Cells.RemoveAt(1);
            GridViewRow rowHeader = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Normal);
            rowHeader.BackColor = System.Drawing.Color.White;
            rowHeader.Font.Bold = true;

            TableCellCollection cells = e.Row.Cells;
            TableCell headerCell = new TableCell();
            headerCell.HorizontalAlign = HorizontalAlign.Center;
            headerCell.Text = "姓名";
            rowHeader.Cells.Add(headerCell);

            headerCell = new TableCell();
            headerCell.HorizontalAlign = HorizontalAlign.Center;
            headerCell.Text = "学期";

            //竖跨两列
            headerCell.RowSpan = 2;
            rowHeader.Cells.Add(headerCell);

            headerCell = new TableCell();
            headerCell.Text = "学生成绩";

            //学生成绩列横跨剩下几个单元格
            headerCell.ColumnSpan = cells.Count - 1;
            headerCell.HorizontalAlign = HorizontalAlign.Center;

            rowHeader.Cells.Add(headerCell);
            rowHeader.Visible = true;
            GridView1.Controls[0].Controls.AddAt(0, rowHeader);
        }
    }

//在DataBound事件中把相同的名字合并

    protected void GridView1_DataBound(object sender, EventArgs e)
    {
        for (int i = 0; i < GridView1.Rows.Count; )
        {
            string nowValue = GridView1.Rows[i].Cells[0].Text;
            int j = i + 1;
            for (; j < GridView1.Rows.Count; j++)
            {
                if (nowValue == GridView1.Rows[j].Cells[0].Text)
                    GridView1.Rows[j].Cells.RemoveAt(0);
                else
                    break;
            }
            if ((j - i) > 1) //如果只有一列就不用RowSpan了,虽然用了没影响,但还是多生成一点html代码
            {
                GridView1.Rows[i].Cells[0].RowSpan = j - i;
            }
            i = j;
        }

    }