ASPxGridView-单元格合并

<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));
    }
}

时间: 2024-09-19 08:51:44

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

DataGridView单元格合并

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

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

1.devexpress实现单元格合并非常的简单,只要设置属性[AllowCellMerge=True]就可以了,实现效果如下图: 2.但是在具体要求中并非需要所有的相同单元格都合并,可能需要其他的条件来控制合并.这个时候我们就需要在事件gridView1_CellMerge中来控制了.下图为根据最后一列判断是否合并单元格的效果图(其中第四列设置为不合并<非必需>,这里只是为了达到一个比较效果.). 3.重要代码: int row1 = e.RowHandle1; int row2 = e.R

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

var newArr = [ {"BranchID":1,"BranchName":"城二","BranchFullName":"城二分公司","IssueTypeID":101,"IssueTypeName":"宏蜂窝连片弱覆盖","Total":242,"WithoutDemand":139,"

JTable 单元格合并 【转】

单元格合并 一.单元格合并.(1)我们可以使用Jtable的三个方法:getCellRect(),columnAtPoint(),and rowAtPoint().第一个方法返回一个单元格的边界(Rectangle类),第二.三个方法分别返回屏幕指定位置的列和行.为了实现单元格合并,我们需要重载(overwrite)这三个方法. (2)另外我们需要找出渲染Jtable的ComponentUI对象,并且修改它以达到我们的目的. (3)创建新的类记录单元格合并情况的数据模型,它要包涵一个方法来取得单

NPOI 生成Excel (单元格合并、设置单元格样式:字段,颜色、设置单元格为下拉框并限制输入值、设置单元格只能输入数字等)

NPIO源码地址:https://github.com/tonyqus/npoi NPIO使用参考:源码中的 NPOITest项目 下面代码包括: 1.包含多个Sheet的Excel 2.单元格合并 3.设置单元格样式:字段,颜色 4.设置单元格为下拉框并限制输入值 5.设置单元格只能输入数字 // // GET: /Excel/ public ActionResult Write() { var workbook = new HSSFWorkbook();//从流内容创建Workbook对象

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

1.   表格单元格合并组件 1.1.      效果展示 1.1.1.    页面展现表格合并单元格 图 4.1 1.1.2.    导出excel合并单元格 图 4.2 1.2.      调用说明 1.2.1.    表格合并单元格调用说明 首先,要有一个在viewModel中绑定的表格,例如,我们有一个绑定对象为this.grid的表格 我们要在表格的onLoadSuccess事件中添加一个方法 snf.mergeCellsByParentField ("grid", &quo

mysql GROUP_CONCAT 函数 将相同的键的多个单元格合并到一个单元格

mysql GROUP_CONCAT 函数 将相同的键的多个单元格合并到一个单元格 MemberID MemberName FruitName -------------- --------------------- -------------- 1 Al Apple 1 Al Cherry Desired output MemberID MemberName FruitName ----------- -------------- ------------ 1 Al Apple, Cherry

自定义控件:DataGridView 单元格合并和二维表头

DataGridView单元格合并和二维表头应用: //DataGridView绑定数据 DataTable dt = new DataTable(); dt.Columns.Add("1"); dt.Columns.Add("2"); dt.Columns.Add("3"); dt.Columns.Add("4"); dt.Rows.Add("中国", "上海", "5000

FastReport单元格合并

FastReport 自带的单元格合并功能为“抑制重复值”,功能真的很弱,还不如不用.网上也有不少解决方案,不过用来用去,都不尽人意. 下面是网上的两个方案: 1.报表脚本中写代码解决:单元格合并 2.修改相关源代码解决:单元格合并 最初我是两种方案结合使用,第1种方案的缺点是很明显的,有时需要写很多的代码,一不小心容易出错:第2种方案也存在问题,似乎只处理第一页的的情况,当有多页时,就乱套了. 由于本人多个软件都是使用fastreport报表控件,随着时间推移,报表越来越多,设计报表也带来了很

修改TreeList单元格格式(实现类似单元格合并效果)

关键点:(1)TreeList中显示的单元格默认不显示上.下.左.右边框,显示的是TreeList自身的行横边框.列纵边框,具体对应TreeList属性中OptionView项下的ShowVertLines.ShowHorzLines两项,将其对应默认值由默认False改为True即可去除行横边框.列纵边框,然后设置怎样的单元格格式显示什么样的单元格格式: (2)在*_CustomDrawNodeCell中修改函数,而不是*_NodeCellStyle中修改,另外需要注意,前者在后者前运行,因此