Repeater control to aggregate data?

0 votes
asked Dec 15, 2008 by bill

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?

4 Answers

0 votes
answered Dec 15, 2008 by galwegian

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

0 votes
answered Dec 15, 2008 by bill

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?

0 votes
answered Dec 15, 2008 by joel-coehoorn

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();
}
0 votes
answered Dec 23, 2008 by bill-martin

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

Welcome to Q&A, where you can ask questions and receive answers from other members of the community.
Website Online Counter

...