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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Display Grouping in Repeater or DataList

Status
Not open for further replies.

IT4EVR

Programmer
Feb 15, 2006
462
0
0
US
I would like to know how to represent groups of data in a repeater and/or datalist.

From what I understand these controls do not intrinsically come with this functionality. How would I go about this and would it be better/easier to use the repeater or datalist for this purpose?

The data is basically read-only. I do not have to edit anything. This page is basically serving as a report.

I want to be able to group on a field called ReportMonth, which concatenates the month and year of the incident reported.
 
If it is just a report, and no editing needed, how about using the built in Crystal Reports to show your data. Much easier to do grouping etc in there.

Jim
 
They don't allow Crystal Reports right now, so I have to go with the datalist,datagrid or repeater.
 
If you are using 2.0 you could use master/details forms with a gridview and detailsview.
 
Here's a simple way to show group headers that works well for me:

Code:
<asp:datalist id="dlJobs" runat="server">
					<itemtemplate>
						<%# DisplayHeaderIfNeeded(Container.DataItem("LOCName")) %>
						<asp:hyperlink id=hylCurrentJobs runat="server" font-size="8pt" navigateurl='<%# DataBinder.Eval(Container.DataItem, "JOBUID") %>' width="124px">
							<%# DataBinder.Eval(Container.DataItem, "JOBTitle") %>
						</asp:hyperlink>
					</itemtemplate>
				</asp:datalist>

And, the function in the code-behind file:

Code:
Function DisplayHeaderIfNeeded(ByVal header As String) As String
        Dim output As String = String.Empty

        ' determine if this header has yet to be displayed
        If header <> strLastHeader Then
            ' set the LastHeader to the current header
            strLastHeader = header

            ' display the header name
            output = "<br><font class=GroupHeader>" & header & "</font><br />"
        End If

        Return output

    End Function

Maybe this can help you...

Jason
 
I should point out that the field names bound to the datalist control (like "JOBTitle," for example) are from a datareader used to populate a dataview or datatable, which is then bound to the datalist via a databind command.

I hope this information is helpful to you. There may be better ways, but this has worked well for me...

Jason
 
Galey, I haven't tried this one yet, but does this actually group the data as well?

What I mean is that I would have something like this:


Code:
               Subsystem  Downtime  %Availability
January 2004   server1      2.40          99.65%
               server2      3.00          99.67%

Total Downtime 5.40 hours

March 2004     server1      5.65          95.65%
               server6      3.00          99.67%

Total Downtime 8.65 hours
 
What's the deal with this strLastHeader, I don't see any initialization for it in the code. It's just comparing it to another string later in the code. It's giving me an error.
 
Oops, the strLastHeader was declared and set to " " at the instance level - forgot about that part of the code.

Glad you found something that will work for you.

Jason
 
Yes, this is a very robust solution. They are basically subclassing the standard repeater control, extending it so that it provides grouping headers and grouping totals.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top