Everybody knows that a
DataGrid is a very useful control that is available in ASP.NET. It has many built-in features, such as sorting, paging, etc. However, one useful thing is lacking – the ability to merge cells like it is implemented in MS FlexGrid ActiveX control. In one of my projects, I came across this need and decided to implement it.
Designing a web control is claimed to be an easy thing, but my choice was to develop a reusable class. How to do it?
The Code Itself
There is a well-known ability in .NET framework – binding events to user-defined procedures. In VB.NET, it is achieved by using
AddHandler routine. Upon my class initialization,
GroupGridItem is bound to the
ItemDataBound event of the
So, every time
ItemDataBound event is raised, the control passes to
GroupGridItem method of the class. It is here that all the stuff takes place. The algorithm is pretty simple: for each bound item, it searches previously bound ones and if the content of the respective cells match, it hides current cell and increases the previous visible cell
RowSpan value by one.
The use of the class is simple – just pass the
DataGrid to format and the column indices you'd like to see grouped.
This is only a first version of the class, so it supports only Bound columns. Your comments will be really appreciated.