Solved

Repeat gridview header every 10 rows

Posted on 2015-01-12
4
784 Views
Last Modified: 2015-01-17
Hi all,

I have a gridview in an application and I need to repeat the header row every 10 rows.

I have the following code at the moment:

    Protected Sub objGridView_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs) Handles objGridView.RowDataBound

        If e.Row.RowType = DataControlRowType.DataRow Then

            If e.Row.RowIndex Mod 10 = 0 And e.Row.RowIndex > 0 Then

                Dim objHeaderRow As GridViewRow = New GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Normal)

                For i As Integer = 0 To sender.Columns.Count - 1
                    If sender.Columns(i).Visible = True Then
                        Dim objTC As New TableCell
                        objTC.Text = sender.Columns(i).HeaderText
                        objHeaderRow.Cells.Add(objTC)
                    End If
                Next

                sender.Controls(0).Controls.AddAt(e.Row.RowIndex, objHeaderRow)

            End If

        End If

    End Sub

Open in new window


The problem is that this code only works for part of the GridView, so, when the page loads, I see the header is repeated, but when I scroll down the page, it suddenly stops.

I think it is something to do with the RowIndex value but I'm not sure.

Any help is greatly appreciated.

Thanks.

resourcesys
0
Comment
Question by:resourcesys
  • 2
  • 2
4 Comments
 
LVL 16

Expert Comment

by:Imran Javed Zia
ID: 40544340
Hi,

You have to update to table/grid index range by adding new rows to avoid index related errors.


Personally, I may recommend you to use DataList or Repeater for such customized UI instead of GridView.

Thanks
0
 
LVL 16

Expert Comment

by:Imran Javed Zia
ID: 40544342
0
 

Accepted Solution

by:
resourcesys earned 0 total points
ID: 40544646
The solution is to have a global variable in the code behind to increment every time you add a header row to the GridView, e.g.

Private intRowPlus As Integer = 0

Open in new window


Set intRowPlus to 0 in the Page_Load method, then adjust the Sub so that the AddAt value is the row index plus the intRowPlus value, and increment intRowPlus after adding a header row:

    Protected Sub objGridView_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs) Handles objGridView.RowDataBound

        If e.Row.RowType = DataControlRowType.DataRow Then

            If e.Row.RowIndex Mod 10 = 0 And e.Row.RowIndex > 0 Then

                Dim objHeaderRow As GridViewRow = New GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Normal)

                For i As Integer = 0 To sender.Columns.Count - 1
                    If sender.Columns(i).Visible = True Then
                        Dim objTC As New TableCell
                        objTC.Text = sender.Columns(i).HeaderText
                        objHeaderRow.Cells.Add(objTC)
                    End If
                Next

                sender.Controls(0).Controls.AddAt(e.Row.RowIndex + intRowPlus, objHeaderRow)

                intRowPlus = intRowPlus + 1

            End If

        End If

    End Sub

Open in new window

0
 

Author Closing Comment

by:resourcesys
ID: 40554959
Solution works and gives desired results.
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now