views:

170

answers:

6

I have a table with doctor offices and doctors in those offices that I'm populating to a repeater control. The data coming back is not aggregated. So, if there are 2 doctors in 1 office, there are 2 rows, same office, different doctor.

Is there a way to aggregate the data so the repeater shows 1 office with all of the doctors from that office so I can avoid the duplication?

A: 

I think you'd be best doing the aggregation in the database.

Galwegian
A: 

I was considering that but thought there might be a fancier way to do it.

If aggregating in the db, pass a delimeted column and parse it during databinding?

Bill
+1  A: 

In your aspx markup:

<table><tr><th>Office</th><th>Doctors</th></tr>
<asp:repeater id="Repeater" runat="server" OnItemDataBound="NextItem" ... >
    <ItemTemplate><asp:Literal id="RepeaterRow" runat="server" />
    </ItemTemplate>
</asp:repeater>
<asp:Literal id="LastRow" runat="server" />
</table>

In your code behind:

public class Office
{
    public string OfficeName {get;set;};
    List<string> _doctors = new List<string>();
    public List<string> Doctors {get{ return _doctors; } };

    void Clear()
    {
        OfficeName = "";
        _doctors.Clear();
    }

    public override string ToString()
    {
        StringBuilder result = new StringBuilder("<tr>");

        result.AppendFormat("<td>{0}</td>", OfficeName);

        string delimiter = "";
        result.Append("<td>");
        foreach(string doctor in Doctors)
        {
           result.Append(doctor).Append(delimiter);
           delimiter = "<br/>";
        }

        result.Append("</td></tr>");

        return result.ToString();
    }
}

.

private string CurOffice = "";
private Office CurRecord = new Office();

void NextItem(object sender, RepeaterItemEventArgs e)
{
    if (e.Item.ItemType != ListItemType.Item && e.Item.ItemType != ListItemType.AlternatingItem) return;

    Literal repeaterRow = e.Item.FindControl("RepeaterRow") as Literal;
    if (repeaterRow == null) return;

    DataRow row = ((DataRowView)e.Item.DataItem).Row;

    if ( CurOffice != (string)row["Office"] )
    {
        repeaterRow.Text = CurRecord.ToString();
        repeaterRow.Visible = true;

        CurRecord.Clear();
        CurOffice = row["Office"];
        CurRecord.Office = CurOffice;
    }
    else
        e.Item.Visible = false;

    CurRecord.Doctors.Add((string)row["doctor"]);
}

void Page_PreRender(object sender, EventArgs e)
{
    LastRow.Text = CurRecord.ToString();
}
Joel Coehoorn
A: 

WOW! Just amazing. Works PERFECTLY! Thanks!

Bill
Make sure you hook up the pre-render event for the last method from the code, or you'll be missing a row.
Joel Coehoorn
Also: as I look at it I think I forgot to set the initial OfficeName.
Joel Coehoorn
A: 

How would you do this in a database?

Bill
A: 

Use the SQL coalesce function

As from SQLTeam.com:

DECLARE @EmployeeList varchar(100)

SELECT @EmployeeList = COALESCE(@EmployeeList + ', ', '') + CAST(Emp_UniqueID AS varchar(5)) FROM SalesCallsEmployees WHERE SalCal_UniqueID = 1

This will create a comma delimeted list of employees. You can do this for doctors too

asp316