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

Listview Grouping with Header and Footer

Status
Not open for further replies.

joshbula

Technical User
Nov 19, 2004
45
0
0
US
Hello,
I am using the following Listview:
Code:
    <asp:ListView ID="ListView1" runat="server" DataSourceID="sqlJudgeRatings">
        <ItemTemplate>
        <b><%#EntryInfoGroup()%> </b>
            <td><font size="1px"><asp:Label ID="JudgeRatingLabel" runat="server" 
                Text='<%# Eval("JudgeRating") %>' /></font>
             </td>   
         <%#AddFinalRating() %> 
     </ItemTemplate>
        <LayoutTemplate>
        <table runat="server" border="1">
          <tr ID="itemPlaceholder" runat="server">
          </tr>
       </table>
            </div>
        </LayoutTemplate>
    </asp:ListView>

And this function to group the data and create a "Header" which is actually a few cells at the beginning of each row of the table:

Code:
    Protected Function EntryInfoGroup() As String
        'Get the data field value of interest for this row
        Dim currentEntryName As String = Eval("EntryName").ToString()
        Dim SchoolName As String = Eval("SchoolName").ToString()
        Dim Directors As String = Eval("Directors").ToString()
        Dim Classification As String = Eval("Classification").ToString()
        Dim FinalRating As String = Eval("FinalRating").ToString()

        If FinalRating.Length = 0 Then
            FinalRating = "NotEntered"
        End If

        'See if there's been a  change in EntryName
        If lastEntryName <> currentEntryName Then
            'There's been a change, Record change and emit the table row
            lastEntryName = currentEntryName
            Return String.Format("</tr><tr><td> {0} {1} <br />Directors: {2}</td><td>&nbsp;&nbsp; {3} &nbsp;&nbsp;</td><td>Final Rating:<br /><b> {4}</td><td>&nbsp;</td> ", SchoolName, currentEntryName, Directors, Classification, FinalRating)
        Else
            'no change, return an empty string
            Return String.Empty
        End If
    End Function


And what I'm trying to do is have this code put a cell at the end of each row of the table with the "Final Rating" in it....

Code:
   Protected Function AddFinalRating() As String
        'Get the data field value of interest for this row
        Dim currentEntryName2 As String = Eval("EntryName").ToString()
        Dim FinalRating As String = Eval("FinalRating").ToString()

        If FinalRating.Length = 0 Then
            FinalRating = "NotEntered"
        End If

        'See if there's been a  change in value
        If lastEntryName2 <> currentEntryName2 Then
            'There's been a change, Record change and emit the table row
            lastEntryName2 = currentEntryName2

            Return String.Format("<td>{0}</td>", FinalRating)
        Else
            'no change, return an empty string
            Return String.Empty
        End If

    End Function

I'm calling it in the asp code right before the end of the itemTemplate, so it is adding it after the first "JudgeRating" in the row, but I want it to wait and not display it until the last "JudgeRating" so it will actually be the last cell in the row. Is there something I can add to the VB code to make it wait until it binds the entire row, or is there somewhere else I should call it from in the asp code?

Thanks for your help!
...Josh

 
I see 2 issues that are complicating the issue.
1. you are grouping too late. the grouping should be done to the data before binding the data to the listview. once bound it's simply plugging the properties/field into the template.
after the data is grouped you can use a nested listviews to bind the data.
2. The listview is the template and the html should be located in the markup, not the code behind built with strings.

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
 
Thanks Jason,
I understand the problems you pointed out, I'm just not sure I understand how to solve them. How do I group the data before binding it to the listview?
 
using code.
1st. I see you are using a datasource control. drop this in favor of actually writing the data access code.

then, depending on whether you use explicit objects or a datatable/set you can run a series of queries against the database to populate the data structure. then bind the data structure to the listview.

Datasources usually return a single result set, which usually creates a cartesian product. You want to group the data (thus removing the cartesian). If this is infact the case run multiple/smaller queries against the database and relate the results together. Dataset with tables/relations handle this well, or you can map the results to your own custom objects.

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top