以上列表中运用的都是基本的东东: 1.多表头; 2.按值改变行颜色; 3.分页码 代码: AndyGridViewTHeaderHepler.cs //-----------------------------------------------------------------------------------
//
// Created: 23:07 2008/9/28    Author: Andy Lu
// File: AndyGridViewTHeaderHepler.cs
// Description: 动态生成复合表头帮助类
// 相邻父列头之间用'#'分隔,父列头与子列头用空格(' ')分隔,相邻子列头用逗号分隔(',').
// 两行:序号#分公司#组别#本日成功签约单数 预警,续约,流失,合计#累计成功签约单数 预警,续约,流失,合计#任务数#完成比例#排名
// 三行:等级#级别#上期结存 件数,重量,比例#本期调入 收购调入 件数,重量,比例#本期发出 车间投料 件数,重量,比例#本期发出 产品外销百分比 件数,重量,比例#平均值
// 三行时请注意:列头要重复
//
//-----------------------------------------------------------------------------------
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using System.Collections;

namespace SQLServerDAL
{
    public class AndyGridViewTHeaderHepler
    {
        public AndyGridViewTHeaderHepler()
        {
            //
            // TODO: Add constructor logic here
            //
        }
        /**//// <summary>
        /// 重写表头
        /// </summary>
        /// <param name="targetHeader">目标表头</param>
        /// <param name="newHeaderNames">新表头</param>
        /// <remarks>
        /// </remarks>
        public void SplitTableHeader(GridViewRow targetHeader, string newHeaderNames)
        {
            TableCellCollection tcl = targetHeader.Cells;//获得表头元素的实例
            tcl.Clear();//清除元素
            int row = GetRowCount(newHeaderNames);
            int col = GetColCount(newHeaderNames);
            string[,] nameList = ConvertList(newHeaderNames,row,col);
            int RowSpan = 0;
            int ColSpan = 0;
            for (int k = 0; k < row; k++)
            {
                string LastFName = "";
                for (int i = 0; i < col; i++)
                {
                    if (LastFName == nameList[i, k] && k!=row-1)
                    {
                        LastFName = nameList[i, k];
                        continue;
                    }
                    else
                    {
                        LastFName = nameList[i, k];
                    }
                    int bFlag=IsVisible(nameList, k, i, LastFName);
                    switch (bFlag)
                    {
                        case 0:
                            break;
                        case 1:
                            RowSpan = GetSpanRowCount(nameList,row, k, i);
                            ColSpan = GetSpanColCount(nameList,row,col, k, i);
                            tcl.Add(new TableHeaderCell());//添加表头控件
                            tcl[tcl.Count - 1].RowSpan = RowSpan;
                            tcl[tcl.Count - 1].ColumnSpan = ColSpan;
                            tcl[tcl.Count - 1].HorizontalAlign = HorizontalAlign.Center;
                            tcl[tcl.Count - 1].Text = LastFName;
                            break;
                        case -1:
                            string[] EndColName = LastFName.Split(new char[] { ',' });
                            foreach(string eName in EndColName){
                                tcl.Add(new TableHeaderCell());//添加表头控件
                                tcl[tcl.Count - 1].HorizontalAlign = HorizontalAlign.Center;
                                tcl[tcl.Count - 1].Text = eName;
                            }
                            break;
                    }
                }
                if (k != row-1)
                {//不是起始行,加入新行标签
                    tcl[tcl.Count - 1].Text = tcl[tcl.Count - 1].Text+"</th></tr><tr class="+targetHeader.CssClass+">";
                }
            }
        }
        /**//// <summary>
        /// 如果上一行已经输出和当前内容相同的列头,则不显示
        /// </summary>
        /// <param name="ColumnList">表头集合</param>
        /// <param name="rowIndex">行索引</param>
        /// <param name="colIndex">列索引</param>
        /// <returns>1:显示,-1:含','分隔符,0:不显示</returns>
        private int IsVisible(string[,] ColumnList,int rowIndex, int colIndex,string CurrName)
        {
            if (rowIndex!=0){
                if (ColumnList[colIndex,rowIndex-1]==CurrName){
                    return 0;
                }else{
                    if (ColumnList[colIndex, rowIndex].Contains(","))
                    {
                        return -1;
                    }else{
                        return 1;
                    }
                }
            }
            return 1;
        }
        /**//// <summary>
        /// 取得和当前索引行及列对应的下级的内容所跨的行数
        /// </summary>
        /// <param name="ColumnList">表头集合</param>
        /// <param name="row">行数</param>
        /// <param name="rowIndex">行索引</param>
        /// <param name="colIndex">列索引</param>
        /// <returns>行数</returns>
        private int GetSpanRowCount(string[,] ColumnList, int row,int rowIndex, int colIndex)
        {
            string LastName = "";
            int RowSpan = 1;
            for (int k = rowIndex; k < row; k++)
            {
                if (ColumnList[colIndex,k]==LastName){
                    RowSpan++;
                }else{
                    LastName = ColumnList[colIndex, k];
                }
            }
            return RowSpan;
        }
        /**//// <summary>
        /// 取得和当前索引行及列对应的下级的内容所跨的列数
        /// </summary>
        /// <param name="ColumnList">表头集合</param>
        /// <param name="row">行数</param>
        /// <param name="col">列数</param>
        /// <param name="rowIndex">行索引</param>
        /// <param name="colIndex">列索引</param>
        /// <returns>列数</returns>
        private int GetSpanColCount(string[,] ColumnList,int row, int col,int rowIndex, int colIndex)
        {
            string LastName = ColumnList[colIndex, rowIndex] ;
            int ColSpan = ColumnList[colIndex, row-1].Split(new char[] { ',' }).Length;
            ColSpan = ColSpan == 1 ? 0 : ColSpan;
            for(int i=colIndex+1;i<col;i++)
            {
                if (ColumnList[i, rowIndex] == LastName)
                {
                    ColSpan += ColumnList[i, row - 1].Split(new char[] { ',' }).Length;
                }
                else
                {
                    LastName = ColumnList[i, rowIndex];
                    break;
                }
            }
            return ColSpan;
        }
        /**//// <summary>
        /// 将已定义的表头保存到数组
        /// </summary>
        /// <param name="newHeaders">新表头</param>
        /// <param name="row">行数</param>
        /// <param name="col">列数</param>
        /// <returns>表头数组</returns>
        private string[,] ConvertList(string newHeaders, int row, int col)
        {
            string[] ColumnNames = newHeaders.Split(new char[] {'#'});
            string[,] news = new string[col, row];
            string Name = "";
            for (int i = 0; i < col; i++)
            {
                string[] CurrColNames = ColumnNames[i].ToString().Split(new char[] { ' ' });
                for (int k = 0; k < row; k++)
                {
                    if (CurrColNames.Length - 1 >= k)
                    {
                        if (CurrColNames[k].Contains(","))
                        {
                            if (CurrColNames.Length != row)
                            {
                                if (Name == "")
                                {
                                    news[i, k] = news[i, k - 1];
                                    Name = CurrColNames[k].ToString();
                                }
                                else
                                {
                                    news[i, k + 1] = Name;
                                    Name = "";
                                }
                            }else{
                                news[i, k] = CurrColNames[k].ToString();
                            }
                        }else{
                            news[i, k] = CurrColNames[k].ToString();
                        }
                    }else{
                        if (Name == "")
                        {
                            news[i, k] = news[i, k - 1];
                        }else{
                            news[i, k] = Name;
                            Name = "";
                        }
                    }
                }
            }
            return news;
        }
        /**//// <summary>
        /// 取得复合表头的行数
        /// </summary>
        /// <param name="newHeaders">新表头</param>
        /// <returns>行数</returns>
        private int GetRowCount(string newHeaders)
        {
            string[] ColumnNames = newHeaders.Split(new char[] { '#' });
            int Count = 0;
            foreach(string name in ColumnNames){
                int TempCount = name.Split(new char[] { ' ' }).Length;
                if (TempCount > Count)
                    Count = TempCount;
            }
            return Count;
        }
        /**//// <summary>
        /// 取得复合表头的列数
        /// </summary>
        /// <param name="newHeaders">新表头</param>
        /// <returns>列数</returns>
        private int GetColCount(string newHeaders)
        {
            return newHeaders.Split(new char[] { '#' }).Length;
        }
       
    }
}

运用时代码如下:

protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
        {
              if (e.Row.RowType == DataControlRowType.Header)
            {
                SQLServerDAL.AndyGridViewTHeaderHepler dHelper = new SQLServerDAL.AndyGridViewTHeaderHepler();

       string header = "序号#分公司#组别#本日成功签约单数 预警,续约,流失,合计#累计成功签约单数 预警,续约,流失,合计#任务数#完成比例#排名";                 //---------------------------------------------------------------------------------------
                // 加载 gridview Header 的样式
                //---------------------------------------------------------------------------------------
                e.Row.CssClass = GridView1.HeaderStyle.CssClass;                 dHelper.SplitTableHeader(e.Row, header);
             }

    }

asp.net GridView实现多表头类 多行表头实现方法的更多相关文章

  1. DevExpress GridControl复合表头(多行表头)设置

    关于DevExpress.XtraGrid的复合表头或多行表头的示例,界面如下图所示 1.首先要把DevExpress的GridControl转换为BandedGridView 2.设置显示列及绑定的 ...

  2. 三步轻松搞定delphi中CXGRID手动添加复表头(多行表头,报表头)

    网上有代码动态生成cxgrid多行表头的源码,地址为:http://mycreature.blog.163.com/blog/static/556317200772524226400/ 如果要手动设计 ...

  3. java excel导出(表头合并,多行表头)

    @RequestMapping(value="orderExcelList2") public void orderExcelList2forJava(Order order,Ht ...

  4. devexpress实现多行表头(复合表头),附源代码

    在许多项目中都会涉及到复合表头(多行表头),这里给大家分享一个devexpress实现多重表头的案例. 1.第一步将表格类型由默认的gridview变为bandedgridview,如图所示: 2.第 ...

  5. 如何让Gridview在没有数据的时候显示表头(asp.net)

    原文:如何让Gridview在没有数据的时候显示表头(asp.net) 1.前言 当对GridView控件进行数据绑定时,如果绑定的记录为空,网页上就不显示GridView,造成页面部分空白,页面布局 ...

  6. asp.net GridView多行表头的实现,合并表头

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

  7. Asp.net GridView 72般绝技

    快速预览:GridView无代码分页排序GridView选中,编辑,取消,删除GridView正反双向排序GridView和下拉菜单DropDownList结合GridView和CheckBox结合鼠 ...

  8. 【DevExpress v17.2新功能预告】增强ASP.NET GridView的功能

    在下一个主要版本v17.2中,我们将为DevExpress ASP.NET GridView添加一些优秀的新功能.在本文中为大家介绍的所有功能都可用于 GridView的ASP.NET WebForm ...

  9. ASP.net如何保证EF操作类线程内唯一

    说到线程内唯一,肯定会想到单例模式,但是如果多用户访问网站就会出现问题.ASP.net中有两种方法可以保证EF操作类线程内唯一(目前只会这两种,以后有好的方法再添加): 1.httpcontext(实 ...

随机推荐

  1. JAVA-JSP内置对象之session范围

    相关资料:<21天学通Java Web开发> session范围1.就是指客户浏览器与服务器一次会话范围内,如果和服务器断开连接,那么这个属性也就失效了.2.通过使用session的set ...

  2. Node.js学习笔记(6)--异步变同步

    说明(2017-5-3 14:59:03): 1. 异步变同步: var fs = require("fs"); var documents = []; fs.readdir(&q ...

  3. [转]JAVA反射中的getFields()方法和getDeclaredFields ()方法的区别

    关于获取类的字段有两种方式:getFields()和getDeclaredFields().我们先来看看这两者的区别吧: getFields():获得某个类的所有的公共(public)的字段,包括父类 ...

  4. centos 7 64安装mongodb

    官网连接地址:https://www.mongodb.com/download-center?jmp=nav#community 列表中没有CentOS!!我选的是这个Linux 64-bit leg ...

  5. Linxu 安装Nignx

    http://www.myhack58.com/Article/sort099/sort0102/2014/44894.htm 1.为了追加 nginx 的 yum 仓库,需要创建一个文件 /etc/ ...

  6. PHP——大话PHP设计模式——基本设计模式(工厂模式、单例模式、注册器模式)

  7. NLP-训练个model出来写诗

    2018年新年,腾讯整出来个ai春联很吸引眼球,刚好有个需求让我看下能不能训出来个model来写出诗经一样的文风,求助了下小伙伴,直接丢过来2个github,原话是: 查了一下诗经一共38000个字, ...

  8. SpringBoot 中 @RequestBody的正确使用方法

    SpringBoot 中 @RequestBody的正确使用方法 最近在接收一个要离职同事的工作,接手的项目是用SpringBoot搭建的,其中看到了这样的写法: @RequestMapping(&q ...

  9. 再谈git的http服务-权限控制hooks版

    通过git-http-backend方法提供的http服务基本上可以做到认证用户才能使用,但只能控制到服务器路径访问,而且无法区分读写.经过不懈努力,找到了方法,相关脚本及配置见后. 基本思路就是利用 ...

  10. 安卓程序代写 网上程序代写[原]Call requires API level 8 (current min is 1)错误

    导入了一个程序 , 每次运行之后都会出现该错误 . 点击clean 错误就会消失 , 但是执行该错误的时候该错误就会重新出现 . 这个错误需要在AndroidManifest.xml配置文件中修改 u ...