Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Simple DataGrid grouping

0.00/5 (No votes)
14 Dec 2003 1  
The given class provides a simple way of grouping DataGrid rows as it is done in MS FlexGrid ActiveX control.

Sample Image - Preview.jpg

Introduction

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 DataGrid control.

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.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here