Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Repeating a header for a datagrid

Status
Not open for further replies.

link9

Programmer
Nov 28, 2000
3,387
US
Has anyone out there gotten the header on a datagrid to repeat?

I have a report that I would like to simply repeat the header every 6 lines, and am having a helluva time getting it to work.

??
penny1.gif
penny1.gif
 
can't you programmatically add a row in the ItemCreated event of the datagrid?

That event is called for every row. Adding a counter that specifies every how many rows you would like the header to repeat will allw you to start an if statement where you add your row.

hth
 
Well, I certainly haven't been able to figure out how to do it...

I don't even see how I might add a row period to the datagrid unless I add it to the dataset (which I can't do in this situation), much less insert one every six rows.

Don't suppose you might have any sample code I might take a peek at, would you? On how to add a row to a dataset? :)
penny1.gif
penny1.gif
 

// C# code to add a row to the dataset
DataSet ds = (DataSet) Session["DataSet"];
DataRow drAdd;
drAdd = ds.Tables["MyTable"].NewRow();
ds.Tables["MyTable"].Rows.Add(drAdd);
ds.Tables["MyTable"].AcceptChanges();

here I am getting back the dataset I am using from a session variable for eg.

hth
 
I don't even see how I might add a row period to the datagrid unless I add it to the dataset (which I can't do in this situation)

The reason I can't is that the grid is sortable. Way to much overhead, IMHO, just to get this to work like I want. I'm just about convinced that you can't do this to the datagrid directly.

Instead, my approach is going to be to re-write the user control that consumes the datagrid object and set it to use a repeater or a datalist instead... probably the repeater, since it's the most customizeable.

thx, though.
penny1.gif
penny1.gif
 
sorry that didn't help, I was answering to the:
"On how to add a row to a dataset? " Question.
The repeater might be the right option for you.
Happy Coding!

 
Yea, I think so. In fact, I'm in the midst of writing a user control right now that you'll be able to use in much the same way as a datagrid -- I'm just going to implement it using a repeater under the covers.

Except it will have a repeat header option -- I'll try to expose many of it's properties just like a datagrid.

I'll throw it out here for download when I get it working. Surely someone else needs something like this.
penny1.gif
penny1.gif
 
Sounds like a charm. I'll be looking foward to try it out.
 
Ok... here it is. There's one thing I'm not happy with, though. Currently, I am not able to actually copy the header row of the datagrid for insertion. What I am able to do is pass in HTML that equates to the row, and use that to insert. So if anyone feels bold, please set me on the straight and narrow.
Code:
Imports System
Imports System.ComponentModel
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls

Namespace cmi.bin.customControls

    <ToolboxData(&quot;<{0}:cmiReversableGrid runat=server></{0}:cmiReversableGrid>&quot;)> Public Class cmiReversableGrid
        Inherits System.Web.UI.WebControls.DataGrid

        Private mvarrepeatHeader As Boolean = 0
        Private mvarrepeatInterval As Integer = 5
        Private mvarheaderText As New String(String.Empty)
        Private rowCounter As Integer = 0

        Protected Overrides Sub Render(ByVal output As System.Web.UI.HtmlTextWriter)
            MyBase.Render(output)
        End Sub

        Private Sub insertHeader(ByVal o As Object, ByVal e As DataGridItemEventArgs) Handles MyBase.ItemCreated
            If mvarrepeatHeader Then
                If e.Item.ItemType = Web.UI.WebControls.ListItemType.Header Then
                    rowCounter = 0
                ElseIf e.Item.ItemType <> Web.UI.WebControls.ListItemType.Footer _
                And e.Item.ItemType <> Web.UI.WebControls.ListItemType.Pager _
                And e.Item.ItemType <> Web.UI.WebControls.ListItemType.Separator Then
                    rowCounter += 1
                    If rowCounter Mod mvarrepeatInterval = 0 Then
                        addRow(o, e)
                    End If
                End If
            End If
        End Sub
        Private Sub addRow(ByVal o As Object, ByVal e As DataGridItemEventArgs)
            Dim dg As DataGrid = o
            Dim tc As New TableCell()
            tc.Controls.Add(New LiteralControl(mvarheaderText))
            Dim di As New DataGridItem(e.Item.ItemIndex + 1, 0, ListItemType.Item)
            di.Cells.Add(tc)
            Dim t As Table = dg.Controls(0)
            t.Rows.Add(di)
        End Sub

        Public ReadOnly Property sortExpression() As String
            Get
                Dim output As String
                output = sortField
                If Not sortAscending Then
                    output &= &quot; DESC&quot;
                End If
                Return output
            End Get
        End Property

        Public Property sortField() As String
            Get
                Dim o As Object = ViewState.Item(&quot;sortField&quot;)
                Dim output As String
                If o Is Nothing Then
                    output = String.Empty
                Else
                    output = o.ToString()
                End If
                Return output
            End Get
            Set(ByVal Value As String)
                If Value = sortField Then
                    sortAscending = Not sortAscending
                End If
                ViewState.Item(&quot;sortField&quot;) = Value
            End Set
        End Property
        Public WriteOnly Property repeatHeader() As Boolean
            Set(ByVal Value As Boolean)
                mvarrepeatHeader = Value
            End Set
        End Property
        Public WriteOnly Property repeatInterval() As Integer
            Set(ByVal Value As Integer)
                mvarrepeatInterval = Value
            End Set
        End Property
        Public WriteOnly Property headerText() As String
            Set(ByVal Value As String)
                mvarheaderText = Value
            End Set
        End Property

        Private Property sortAscending() As Boolean
            Get
                Dim o As Object = ViewState.Item(&quot;sortAscending&quot;)
                Dim output As String
                If o Is Nothing Then
                    output = True
                Else
                    output = CBool(o)
                End If
                Return output
            End Get
            Set(ByVal Value As Boolean)
                ViewState.Item(&quot;sortAscending&quot;) = Value
            End Set
        End Property

    End Class

End Namespace

This is actually an extension to the one that I put in the extensability FAQ. So I'll leave the explanation of how to use the reversability to that (faq855-2026).

To use the repeat header function, you must issue three statements:

link9Grid.repeatHeader = true
link9Grid.repeatInterval = 6 'or whatever number
link9Grid.headerText = &quot;<tr><td>Here's my header</td></tr>&quot;

Now to get the header, I'll usually just load it up one time w/o the repeating header, view the source, and copy the header row source -- then I'll paste it in to the codebehind like I've shown there. Not ideal, but not too bad. Just that if you change the grid, you have to change that, too.

hth! :)
paul
penny1.gif
penny1.gif
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top