Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Repeat gridview header every 10 rows

Posted on 2015-01-12
4
Medium Priority
?
1,139 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
In real business world data are crucial and sometimes data are shared among different information systems. Hence, an agreeable file transfer protocol need to be established.
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…

886 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