Displaying Data Grouped by Category

This live demo shows how to display information grouped by a category. Specifically, the players for each team are displayed beneath the team's name. This is accomplished using a DataList, but could be just as easily accomplished using a DataGrid with a TemplateColumn.



Kings
Vlade Divac
Chris Webber
Mike Bibby
Doug Christie

Lakers
Shaq
Rick Fox
Kobe Bryant
Robert Horry

Spurs
Tim Duncan
Terry Parker
David Robinson


Source Code

<%@ Page Language="VB" %>
<%@ Import Namespace="System.Data" %>
<script runat="server">

    Sub Page_Load(sender as Object, e as EventArgs)
         If Not Page.IsPostBack then
             BindData()
         End If
    End Sub
    
    
    Sub BindData()
		'Create the DataTable
		Dim dtTable as New DataTable
		dtTable.Columns.Add(New DataColumn(("FullName"), GetType(String)))
		dtTable.Columns.Add(New DataColumn(("Team"), GetType(String)))
		
		'Create Rows for the FullNames
		Dim dr as DataRow
		dr = dtTable.NewRow()
		dr("FullName") = "Shaq"
		dr("Team") = "Lakers"
		dtTable.Rows.Add(dr)
		
		dr = dtTable.NewRow()
		dr("FullName") = "Rick Fox"
		dr("Team") = "Lakers"
		dtTable.Rows.Add(dr)

		dr = dtTable.NewRow()
		dr("FullName") = "Vlade Divac"
		dr("Team") = "Kings"
		dtTable.Rows.Add(dr)

		dr = dtTable.NewRow()
		dr("FullName") = "Tim Duncan"
		dr("Team") = "Spurs"
		dtTable.Rows.Add(dr)

		dr = dtTable.NewRow()
		dr("FullName") = "Kobe Bryant"
		dr("Team") = "Lakers"
		dtTable.Rows.Add(dr)
		
		dr = dtTable.NewRow()
		dr("FullName") = "Terry Parker"
		dr("Team") = "Spurs"
		dtTable.Rows.Add(dr)
    
		dr = dtTable.NewRow()
		dr("FullName") = "Robert Horry"
		dr("Team") = "Lakers"
		dtTable.Rows.Add(dr)
 
 		dr = dtTable.NewRow()
		dr("FullName") = "Chris Webber"
		dr("Team") = "Kings"
		dtTable.Rows.Add(dr)

		dr = dtTable.NewRow()
		dr("FullName") = "Mike Bibby"
		dr("Team") = "Kings"
		dtTable.Rows.Add(dr)

		dr = dtTable.NewRow()
		dr("FullName") = "David Robinson"
		dr("Team") = "Spurs"
		dtTable.Rows.Add(dr)

		dr = dtTable.NewRow()
		dr("FullName") = "Doug Christie"
		dr("Team") = "Kings"
		dtTable.Rows.Add(dr)


		'Now, create a DataView and order by the team name
		Dim myDataView as DataView = dtTable.DefaultView
		myDataView.Sort = "Team"
		
		'Now, bind the DataView to the DataGrid
		dlPlayers.DataSource = myDataView
		dlPlayers.DataBind()
	End Sub
	
	
	Dim lastUsedTeam as String = String.Empty
	
	Function DisplayTeamIfNeeded(team as String) as String
		Dim output as String = String.Empty
		If team <> lastUsedTeam then
			'Set that the lastUsedTeam is the current team value
			lastUsedTeam = team
		
			'Display separator!
			output = "<br /><b>" & team & "</b><br />"   'Display the team name
		End If

		Return output			
	End Function
</script>


	<asp:DataList runat="server" id="dlPlayers">
	  <ItemTemplate>
	       <%# DisplayTeamIfNeeded(Container.DataItem("Team")) %>
	       <%# Container.DataItem("FullName") %>

	  </ItemTemplate>
	</asp:DataList>
    

[Return to the FAQ...]