Solved

Repeat gridview header every 10 rows

Posted on 2015-01-12
4
943 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

738 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