Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Repeat gridview header every 10 rows

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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
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…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

705 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