中继器控制聚合数据?

时间:2008-12-15 17:07:25

标签: c# asp.net sql sql-server sql-server-2005

我在那些办公室里有一个医生办公室和医生的桌子,我正在填写转发器控件。返回的数据未汇总。所以,如果1个办公室有2名医生,则有2排,同一个办公室,不同的医生。

有没有办法汇总数据,以便转发器与该办公室的所有医生一起显示1个办公室,这样我可以避免重复?

4 个答案:

答案 0 :(得分:1)

在你的aspx标记中:

<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>

在你的代码背后:

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();
}

答案 1 :(得分:0)

我认为你最好在数据库中进行聚合。

答案 2 :(得分:0)

我正在考虑这一点,但认为可能有更好的方法来做到这一点。

如果在db中聚合,则传递一个delimeted列并在数据绑定期间解析它?

答案 3 :(得分:0)

使用SQL合并功能

从SQLTeam.com开始:

DECLARE @EmployeeList varchar(100)

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

这将创建一个逗号分隔的员工列表。你也可以为医生这样做