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.
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.
file:
.ascx
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.
file:
.cs
Particular code for GridView:
and for DataGrid:
Next code is common for both GridView and DataGrid:
That is all. The code can be used without any modification, the only part that has to be changed in a concrete case is:
From http://marss.co.ua/MergingGridViewHeaderColumns.aspx
好东东,先与各位分享,有时间再翻译.