Monday, March 26, 2012

Suggestions for ASP.NET Data Repeater

Right. Any suggestions on how to layout data to look like the following? I tried using a a web user control with a repeater in it, and for each different location create a new user web control and bind only the establishments applicable to that location to that repeater... but for some reason the repeaters or any other data controls aren't instantiated inside the web user control. Total pain in the bum.

The salient fields in the Establishments table in relation to the image are:
ID, Name, Area

The ID is just the ID for each establishment, the name is the name (e.g. "Arklow Bay Confer...", "Glenart Castle Hotel", "Ballyknocked Coun..." etc. etc."), and the Area (e.g. Arklow, Ashford, Aughrim, Blessington).

Any thoughts on how I could design my Repeater or Grid etc to display everything, but to group by the Area field as in the image?This is coming from a database, correct? And you have control over the recordset that is returned?
Absolute and complete control yes
Have two datatables filled up in your dataset.

First:

EstablishmentID+LongName

Second:

EstablishmentID+Area

You can then add a relationship between them, and bind it to the repeater. Styling it as you wish of course.

Here's an example:
http://support.microsoft.com/default.aspx?scid=kb;en-us;326338
Cool thanks I'll have a look at that!
Post your code when it works. It's helpful for searches. :)
Didn't take too much tinkering to get it working. I'll post a fuller example in a little while...

<asp:Repeater id="parentRepeater" runat="server">
<itemtemplate>
<b>
<%#DataBinder.Eval(Container.DataItem, "[Area Name]")%>
</b>
<br>
<asp:Repeater id="childRepeater" runat="server" datasource='<%#Container.DataItem.Row.GetChildRows("myrelation")%>'>
<itemtemplate>
<%#Container.DataItem("Name")%><br>
</itemtemplate>
</asp:Repeater>
</itemtemplate>
</asp:Repeater>

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim cnn As SqlConnection = New SqlConnection("Data Source=localhost;Initial Catalog=SomeDB;Integrated Security=True;Pooling=False")
Dim cmd1 As SqlDataAdapter = New SqlDataAdapter("select * from areas", cnn)
Dim ds As DataSet = New DataSet()
cmd1.Fill(ds, "areas")

Dim cmd2 As SqlDataAdapter = New SqlDataAdapter("select * from establishments", cnn)
cmd2.Fill(ds, "establishments")

ds.Relations.Add("myrelation", ds.Tables("areas").Columns(0), ds.Tables("establishments").Columns(14))
parentRepeater.DataSource = ds.Tables("areas")

Page.DataBind()
cnn.Close()
End Sub

0 comments:

Post a Comment