<dx:ASPxGridView ID="gridView" runat="server" ClientInstanceName="gvResults" Width="550px"
                AutoGenerateColumns="True" KeyFieldName="OrderID" DataSourceID="AccessDataSource1">
</dx:ASPxGridView>
<asp:AccessDataSource ID="AccessDataSource1" runat="server" DataFile="~/App_Data/nwind.mdb"
                SelectCommand="SELECT * FROM [Orders]"></asp:AccessDataSource>

<script type="text/javascript">
        window.__OriginalDXUpdateRowCellsHandler = ASPxClientTableFixedColumnsHelper.prototype.ChangeCellsVisibility;
        ASPxClientTableFixedColumnsHelper.prototype.ChangeCellsVisibility = function(row, startIndex, endIndex, display) {
            if((row.cells.length == 0) || (row.cells[0].getAttribute("ci") == null))
                window.__OriginalDXUpdateRowCellsHandler(row, startIndex, endIndex - 1, display); // base call
            else {
                //custom processing
                for(var i = startIndex; i <= endIndex; i++) {
                    var cell = FindCellWithColumnIndex(row, i);
                    if(cell != null)
                        cell.style.display = display;
                }
            }
        };

function FindCellWithColumnIndex(row, colIndex) {
            for(var i = 0; i < row.cells.length; i++) {
                if(row.cells[i].getAttribute("ci") == colIndex)
                    return row.cells[i];
            }
            return null;
        }
=================================

protected void Page_Load(object sender, EventArgs e)  {
        if (!(IsPostBack || IsCallback))
            gridView.DataBind();

new ASPxGridViewCellMerger(gridView);

gridView.Columns[0].FixedStyle = GridViewColumnFixedStyle.Left;
        gridView.Columns[1].FixedStyle = GridViewColumnFixedStyle.Left;

gridView.Columns[0].CellStyle.BackColor = Color.FromArgb(0xEE, 0xEE, 0xEE);
        gridView.Columns[1].CellStyle.BackColor = Color.FromArgb(0xEE, 0xEE, 0xEE);

gridView.Settings.ShowHorizontalScrollBar = true;
    }

===========CellMerger.cs=====================

using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DevExpress.Web.ASPxGridView;
using System.Collections.Generic;

public class ASPxGridViewCellMerger {
    ASPxGridView grid;
    Dictionary<GridViewDataColumn, TableCell> mergedCells = new Dictionary<GridViewDataColumn, TableCell>();
    Dictionary<TableCell, int> cellRowSpans = new Dictionary<TableCell, int>();

public ASPxGridViewCellMerger(ASPxGridView grid) {
        this.grid = grid;
        Grid.HtmlRowCreated += new ASPxGridViewTableRowEventHandler(grid_HtmlRowCreated);
        Grid.HtmlDataCellPrepared += new ASPxGridViewTableDataCellEventHandler(grid_HtmlDataCellPrepared);
    }

public ASPxGridView Grid { get { return grid; } }
    void grid_HtmlDataCellPrepared(object sender, ASPxGridViewTableDataCellEventArgs e) {
        //add the attribute that will be used to find which column the cell belongs to
        e.Cell.Attributes.Add("ci", e.DataColumn.VisibleIndex.ToString());
        
        if (cellRowSpans.ContainsKey(e.Cell)) {
            e.Cell.RowSpan = cellRowSpans[e.Cell];
        }
    }
    void grid_HtmlRowCreated(object sender, ASPxGridViewTableRowEventArgs e) {
        if (Grid.GetRowLevel(e.VisibleIndex) != Grid.GroupCount) return;
        for (int i = e.Row.Cells.Count - 1; i >= 0; i--) {
            DevExpress.Web.ASPxGridView.Rendering.GridViewTableDataCell dataCell = e.Row.Cells[i] as DevExpress.Web.ASPxGridView.Rendering.GridViewTableDataCell;
            if (dataCell != null) {
                MergeCells(dataCell.DataColumn, e.VisibleIndex, dataCell);
            }
        }
    }

void MergeCells(GridViewDataColumn column, int visibleIndex, TableCell cell) {
        bool isNextTheSame = IsNextRowHasSameData(column, visibleIndex);
        if (isNextTheSame) {
            if (!mergedCells.ContainsKey(column)) {
                mergedCells[column] = cell;
            }
        }
        if (IsPrevRowHasSameData(column, visibleIndex)) {
            ((TableRow)cell.Parent).Cells.Remove(cell);
            if (mergedCells.ContainsKey(column)) {
                TableCell mergedCell = mergedCells[column];
                if (!cellRowSpans.ContainsKey(mergedCell)) {
                    cellRowSpans[mergedCell] = 1;
                }
                cellRowSpans[mergedCell] = cellRowSpans[mergedCell] + 1;
            }
        }
        if (!isNextTheSame) {
            mergedCells.Remove(column);
        }
    }
    bool IsNextRowHasSameData(GridViewDataColumn column, int visibleIndex) {
        //is it the last visible row
        if (visibleIndex >= Grid.VisibleRowCount - 1)
            return false;

return IsSameData(column.FieldName, visibleIndex, visibleIndex + 1);
    }
    bool IsPrevRowHasSameData(GridViewDataColumn column, int visibleIndex) {
        ASPxGridView grid = column.Grid;
        //is it the first visible row
        if (visibleIndex <= Grid.VisibleStartIndex)
            return false;

return IsSameData(column.FieldName, visibleIndex, visibleIndex - 1);
    }
    bool IsSameData(string fieldName, int visibleIndex1, int visibleIndex2) {
        // is it a group row?
        if (Grid.GetRowLevel(visibleIndex2) != Grid.GroupCount)
            return false;

return object.Equals(Grid.GetRowValues(visibleIndex1, fieldName), Grid.GetRowValues(visibleIndex2, fieldName));
    }
}

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

  1. ExtJS 4.2 Grid组件的单元格合并

    ExtJS 4.2 Grid组件本身并没有提供单元格合并功能,需要自己实现这个功能. 目录 1. 原理 2. 多列合并 3. 代码与在线演示 1. 原理 1.1 HTML代码分析 首先创建一个Grid ...

  2. NPOI 教程 - 2.1单元格合并

    来源:http://liyingchun343333.blog.163.com/blog/static/3579731620091018212990/ 合并单元格在制作表格时很有用,比如说表格的标题就 ...

  3. asp.net使用控件datagrid实现表头单元格合并

    合并的要点: 1.datagid的单元格合并原理是table中tr,td的布局实现; 2.合并的时机实在其datagridcreate事件中实现; 3.认识一个对象TableCellCollectio ...

  4. DataGridView单元格合并

    本文章转载:http://www.cnblogs.com/xiaofengfeng/p/3382094.html 图: 代码就是如此简单 文件下载:DataGridView单元格合并源码 也可以参考: ...

  5. devexpress实现单元格合并以及依据条件合并单元格

    1.devexpress实现单元格合并非常的简单,只要设置属性[AllowCellMerge=True]就可以了,实现效果如下图: 2.但是在具体要求中并非需要所有的相同单元格都合并,可能需要其他的条 ...

  6. SNF快速开发平台MVC-表格单元格合并组件

    1.   表格单元格合并组件 1.1.      效果展示 1.1.1.    页面展现表格合并单元格 图 4.1 1.1.2.    导出excel合并单元格 图 4.2 1.2.      调用说 ...

  7. 关于table动态添加数据 单元格合并 数组合并

    var newArr = [ {"BranchID":1,"BranchName":"城二","BranchFullName&qu ...

  8. excel技巧--单元格合并与拆分

    如果要将上图的地区列做成下图的合并单一列: 有如下做法: (以下图表格为例) 1.选择要排序的表格,点击“开始”-->排序和筛选-->自定义排序.在对话框选择“业务项目”进行排序: 2.选 ...

  9. element-ui table 最后一行合计,单元格合并

    接着写两个方法--最后一行合计的方法 --单元格合并的方法 先写一个rowspan方法,计算出spanArr数组是怎么单元格合并的,注意rowspan方法要在渲染完成之前使用,可以在mounted中使 ...

  10. 如何通过DataGridView 实现单元格合并和二维表头

    先看下实现出来的效果(这里随便写了几组数据,用来测试) 先初始一个DataGridView 设置哪几列 DataGridView 里男女这两列的 AutoSizeMode 可以设置Fill. publ ...

随机推荐

  1. opennebula extend(expending) auth module ldap

    LDAP Authentication addon permits users to have the same credentials as in LDAP, so effectively cent ...

  2. 优步北京B组(8月10日-8月16日奖励规则)

    奖励政策: [优步北京B组] 定义为2015年7月20日前激活的部分司机(以优步后台数据显示为准) 滴滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机( ...

  3. Linux学习之十四、管线命令

    Linux学习之十四.管线命令 地址:http://vbird.dic.ksu.edu.tw/linux_basic/0320bash_6.php

  4. javascript第七课js函数

    function add() { } 上面就是js中的方法,js中的方法与c#中的方法不同的是不需要写返回值类型 function  add(num1,num2) { return num1+num2 ...

  5. Data Structure(3)——软考阶段学习小结

    数据结构计算机等级考试中有,自考中有,软考中同样有,可见其内容的重要程度,今天对软考阶段视频学习内容的总结,同样是对前面学习内容的回顾,同样是对后面学习的铺垫. 中结:原本因为之前有过类似的总结,这次 ...

  6. linux查看与设置主机名

    1.设置主机名    通过编辑/etc/sysconfig/network文件中的HOSTNAME字段就可以修改主机名.如下所示:     [root@zijuan /]# vim /etc/sysc ...

  7. php7 install script

    ./configure --prefix=/home/admin/local/php7 --with-gd=/home/admin/local/libgd-2.1.1/ --with-jpeg-dir ...

  8. leetcode Remove Element python

    class Solution(object): def removeElement(self, nums, val): """ :type nums: List[int] ...

  9. Why Study JavaScript?

    JavaScript is one of the 3 languages all web developers must learn: 1. HTML to define the content of ...

  10. 关于require,require_once,include和include_once的区别

    一.定义 require,require_once,include,include_once都属于PHP的关键字,也就是说它们实际都是PHP的语句,而不是函数,类似于print,echo一样,也是PH ...