Wednesday, September 6, 2017

// // Leave a Comment

How to merge cells in Gridview VB

In this article I am going to explain how to merge cells in Gridview.

If field data is repeating row wise, when we bind data with Gridview, same record is show more times in Gridview as below. 

123456  Commercial Bank PLC
345632  Commercial Bank PLC
567890  Commercial Bank PLC

I am going to show one record for repeating data as below.

123456  
345632  Commercial Bank PLC
567890  

As previous articles we have to drag and drop Griview controller to design form and bind data using data source.



Open the Gridview RoDataBound event and past the below coding


 Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
        For rowIndex As Integer = GridView1.Rows.Count - 2 To 0 Step -1

            Dim gviewRow As GridViewRow = GridView1.Rows(rowIndex)

            Dim gviewPreviousRow As GridViewRow = GridView1.Rows(rowIndex + 1)

            For cellCount As Integer = 0 To gviewRow.Cells.Count - 1

                If gviewRow.Cells(cellCount).Text = gviewPreviousRow.Cells(cellCount).Text Then

                    If gviewPreviousRow.Cells(cellCount).RowSpan < 2 Then

                        gviewRow.Cells(cellCount).RowSpan = 2

                    Else

                        gviewRow.Cells(cellCount).RowSpan = gviewPreviousRow.Cells(cellCount).RowSpan + 1

                    End If

                    gviewPreviousRow.Cells(cellCount).Visible = False
                End If

            Next

        Next
    End Sub





0 comments:

Post a Comment