Recent Posts

Tuesday, March 31, 2009

How to merge columns of a gridview in Asp net 2.0?

I was playing around gridview to find out the ways to merge the cells in asp net 2.0 gridview as we do in Microsoft excel. After working few hours on it with the help of google i got a function ready for the new programmers to learn how to merge cells in asp net 2.0 gridview which has got identical data.
You can just use the following function to merge the cells of asp net 2.0 gridview as in Microsoft excel:

Public Shared Sub MergeCells(ByVal gridView As GridView)
        For rowIndex As Integer = gridView.Rows.Count - 2 To 0 Step -1
            Dim row As GridViewRow = gridView.Rows(rowIndex)
            Dim previousRow As GridViewRow = gridView.Rows(rowIndex + 1)

            For cellIndex As Integer = 0 To row.Cells.Count - 1
                If row.Cells(cellIndex).Text = previousRow.Cells(cellIndex).Text Then
                    If previousRow.Cells(cellIndex).RowSpan < 2 Then
                        row.Cells(cellIndex).RowSpan = 2
                        row.Cells(cellIndex).RowSpan = previousRow.Cells(cellIndex).RowSpan + 1
                    End If
                    previousRow.Cells(cellIndex).Visible = False
                End If
            Next cellIndex
        Next rowIndex
    End Sub

Related Posts by Categories

No comments:

Post a Comment