打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Merging columns in GridView/DataGrid header

Merging columns in GridView/DataGrid header

6 Jun 2007

Background

As necessity to show header columns in a few rows occurs fairly often it would be good to have such functionality in the GridView/DataGrid control as an in-built feature. But meanwhile everyone solves this problem in his own way.

The described below variant of the merging implementation is based on irwansyah‘s idea to use the SetRenderMethodDelegate method for custom rendering of grid columns header. I guess this approach can be simplified in order to get more compact and handy code for reuse.

The code overview


As it may be required to merge a few groups of columns - for example, 1,2 and 4,5,6 - we need a class to store common information about all united columns.
[Serializable]private class MergedColumnsInfo{// indexes of merged columnspublic List<int> MergedColumns = new List<int>();// key-value pairs: key = the first column index, value = number of the merged columnspublic Hashtable StartColumns = new Hashtable();// key-value pairs: key = the first column index, value = common title of the merged columnspublic Hashtable Titles = new Hashtable();//parameters: the merged columns indexes, common title of the merged columns public void AddMergedColumns(int[] columnsIndexes, string title){MergedColumns.AddRange(columnsIndexes);StartColumns.Add(columnsIndexes[0], columnsIndexes.Length);Titles.Add(columnsIndexes[0], title);}}

Attribute Serializable is added in order to have a possibility to store information about merged columns in ViewState - it is required if paging or sorting is used.
That is the only additional action. Now the code usage.

.ascx file:

//for GridView<asp:GridView ID="grid" runat=server OnRowCreated="GridView_RowCreated" ... ></asp:GridView>//for DataGrid<asp:DataGrid ID="grid" runat=server OnItemCreated="DataGrid_ItemCreated" ... ></asp:DataGrid>

Columns can be defined in design time or can be auto generated - it does not matter and doesn‘t influence the further code. Merging also does not harm sorting and paging if they are used in the GridView/DataGrid.

.cs file:

//property for storing of information about merged columnsprivate MergedColumnsInfo info{get{if (ViewState["info"] == null)ViewState["info"] = new MergedColumnsInfo();return (MergedColumnsInfo)ViewState["info"];}}protected void Page_Load(object sender, EventArgs e){if (!IsPostBack){//merge the second, third and fourth columns with common title "Subjects"info.AddMergedColumns(new int[] { 1, 2, 3 }, "Subjects");grid.DataSource = ... //some data sourcegrid.DataBind();}}

Particular code for GridView:
protected void GridView_RowCreated(object sender, GridViewRowEventArgs e){//call the method for custom rendering the columns headersif (e.Row.RowType == DataControlRowType.Header)e.Row.SetRenderMethodDelegate(RenderHeader);}

and for DataGrid:
protected void DataGrid_ItemCreated(object sender, DataGridItemEventArgs e){//call the method for custom rendering the columns headersif (e.Item.ItemType == ListItemType.Header)e.Item.SetRenderMethodDelegate(RenderHeader);}

Next code is common for both GridView and DataGrid:

//method for rendering the columns headersprivate void RenderHeader(HtmlTextWriter output, Control container){for (int i = 0; i < container.Controls.Count; i++){TableCell cell = (TableCell)container.Controls[i];//stretch non merged columns for two rowsif (!info.MergedColumns.Contains(i)){cell.Attributes["rowspan"] = "2";cell.RenderControl(output);}else //render merged columns common titleif (info.StartColumns.Contains(i)){output.Write(string.Format("<td align=‘center‘ colspan=‘{0}‘>{1}</td>",info.StartColumns[i], info.Titles[i]));}}//close the first rowoutput.Write("</tr>");//set attributes for the second rowgrid.HeaderStyle.AddAttributesToRender(output);//start the second rowoutput.RenderBeginTag("tr");//render the second row (only the merged columns)for (int i = 0; i < info.MergedColumns.Count; i++){TableCell cell = (TableCell)container.Controls[info.MergedColumns[i]];cell.RenderControl(output);}}

That is all. The code can be used without any modification, the only part that has to be changed in a concrete case is:

info.AddMergedColumns(new int[] { 1, 2, 3 }, "Foo");info.AddMergedColumns(new int[] { 6, 7 }, "Bar");//and so forth ...
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
gridview动态添加、删除模版列
WPF DataGrid 之数据绑定
DevExpress Grid控件经典常用功能代码收集
DevExpress的GridControl的使用以及怎样添加列和绑定数据源
c#教程之C#实现GridView导出Excel
grid 列只读
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服