如何在asp.net的datalist控件中对行进行分组?

时间:2012-06-30 14:03:37

标签: asp.net datalist asp.net-controls

例如在我的数据库中有96行,我想要发生的是将行分组为4(结果表将有3列,8行)。前

enter image description here

1 个答案:

答案 0 :(得分:2)

如果您可以使用除DataList之外的控件,那么您可能会有更轻松的时间。例如,您可以使用Table服务器控件,如下所示:

<asp:Table ID="tblGrouped" runat="server"></asp:Table>

然后在代码中:

protected void LoadData() {
    var items = MyDataSource.GetMyItems();

    TableRow tr = null;
    TableCell tc = null;

    for (int i = 0; i < items.Count; i++) {
        if (i % 12 == 0) {
            tr = new TableRow();
            tc = new TableCell();
            tc.Text = items[i].MyProperty;
            tr.Cells.Add(tc);
            tblGrouped.Rows.Add(tr);
        } else if (i % 4 == 0) {
            tc = new TableCell();
            tc.Text = items[i].MyProperty;
            tr.Cells.Add(tc);
        } else {
            tc.Text += "<br />" + items[i].MyProperty;
        }
    }
}