Click here to Skip to main content
15,894,343 members
Articles / Web Development / ASP.NET
Article

Simple DataGrid grouping

Rate me:
Please Sign up or sign in to vote.
3.16/5 (34 votes)
14 Dec 20031 min read 219.7K   3.4K   46   38
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


Written By
Web Developer
Belarus Belarus
I work for Logic Software (http://www.logicsoftware.net) as a lead .NET developer/architect.

Comments and Discussions

 
GeneralRe: Missing IsGroupedCol Function Pin
rcarpin15-Apr-05 8:50
rcarpin15-Apr-05 8:50 
GeneralGood Work Pin
Hemang Shukla22-Sep-04 13:11
Hemang Shukla22-Sep-04 13:11 
GeneralA little tweak! Pin
barsju30-Jul-04 2:20
barsju30-Jul-04 2:20 
GeneralRe: A little tweak! Pin
Serge Lobko-Lobanovsky30-Jul-04 2:30
Serge Lobko-Lobanovsky30-Jul-04 2:30 
GeneralAlign Text in grouped Cell Pin
Member 6479613-Jul-04 1:26
Member 6479613-Jul-04 1:26 
Generalmissing Isgroupcol function Pin
brijesh_Pandya17-Jun-04 5:27
sussbrijesh_Pandya17-Jun-04 5:27 
GeneralRe: missing Isgroupcol function Pin
Serge Lobko-Lobanovsky17-Jun-04 5:34
Serge Lobko-Lobanovsky17-Jun-04 5:34 
GeneralGood Job.. Pin
nickpatel1-Apr-04 10:35
nickpatel1-Apr-04 10:35 
I had to make little changes but above code works fine. I am able to merge rows on datagrid.

Here is my changes and calling app.

Calling App... .aspx file... Merge first and third column data.

Protected WithEvents dgResults As System.Web.UI.WebControls.DataGrid
Dim objCUILGridFormatter As New UILGridFormatter(dgFuelEconomyResults, 0, 3)
Dim ResultsDataView As DataView = ResultsDataTable.DefaultView
ResultsDataView .Sort = "KEY"
dgResults .DataSource = ResultsDataView
dgResults .DataBind()
objCUILGridFormatter = Nothing
ResultsDataView = Nothing
ResultsDataTable = Nothing


Class for merge format...Modified class of serge... CUILGridFormatter.vb

Imports System.Web.UI
Imports System.Web.UI.WebControls

Public Class UILGridFormatter

Private _previtem As DataGridItem
Private WithEvents _dg As DataGrid
Private arrCls() As Integer

Public Sub New(ByRef aobjDg As DataGrid, ByVal ParamArray arrCols() As Integer)
Dim i As Integer
For i = LBound(arrCols) To UBound(arrCols)
ReDim Preserve arrCls(i)
arrCls(i) = arrCols(i)
Trace.Write(arrCls(i) & " ")
Next

_dg = aobjDg
AddHandler _dg.ItemDataBound, AddressOf Me.GroupGridItem
End Sub


' this sub formats the datagrid so that given column is grouped
' (sets its rowspan value) according to the number of other columns
Public Sub GroupGridItem(ByVal sender As Object, ByVal e As DataGridItemEventArgs)
Dim cell As TableCell

If e.Item.ItemType = ListItemType.AlternatingItem Or e.Item.ItemType = ListItemType.Item Then
For Each cell In e.Item.Cells
cell.RowSpan = 1
Next

Dim i As Integer
If Not _previtem Is Nothing Then
For i = 0 To e.Item.Cells.Count - 1
If IsGroupedCol(i) Then
cell = e.Item.Cells(i)
cell.Visible = True

With _previtem
If cell.Text.Trim = .Cells(i).Text.Trim And cell.Text.Trim <> "" Then

Dim a As Integer
For a = e.Item.ItemIndex To 0 Step -1
If a - 1 < 0 Then
Exit For
End If

Dim ditm2 As DataGridItem
ditm2 = _dg.Items(a - 1)
If Not ditm2 Is e.Item Then
If ditm2.Cells(i).Text.Trim = cell.Text.Trim And ditm2.Cells(i).Visible = True Then
ditm2.Cells(i).RowSpan += 1
cell.Visible = False
Exit For
End If
End If
Next a
End If
End With
End If
Next i
_previtem = e.Item
Else
_previtem = e.Item
End If
End If
End Sub

Private Function IsGroupedCol(ByVal col As Integer) As Boolean
Dim retVal As Boolean
Dim i As Integer
For i = 0 To arrCls.Length - 1
If arrCls(i) = col Then
retVal = True
Exit For
End If
Next
Return retVal
End Function

Protected Overrides Sub Finalize()
MyBase.Finalize()
RemoveHandler _dg.ItemDataBound, AddressOf Me.GroupGridItem
End Sub
End Class


GeneralGood Idea. Pin
Chabun25-Dec-03 7:32
Chabun25-Dec-03 7:32 
GeneralA sample file would help. Pin
Cypher17-Dec-03 22:20
Cypher17-Dec-03 22:20 
GeneralRe: A sample file would help. Pin
Serge Lobko-Lobanovsky17-Dec-03 22:41
Serge Lobko-Lobanovsky17-Dec-03 22:41 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.