Solved

Repeat gridview header every 10 rows

Posted on 2015-01-12
4
857 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
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…

821 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