简体   繁体   中英

DataList grouping items by category

I have a datalist and I would like to group my items in 3 columns with one item header(category) like:

[Category A]

[ItemA] [ItemB] [ItemC]

[Category B]

[ItemA] [ItemB] [ItemC]

My query retrieves the items in this structure:

1 CategoryA ItemA 2 CategoryA ItemB 3 CategoryA ItemC 4 CategoryB ItemA 5 CategoryB ItemB 6 CategoryB ItemC

This is what I have, but does not work as expected. Any ideas?

  <ItemTemplate>
       SubCatName:
            <asp:Label ID="SubCatNameLabel" runat="server" Text='<%# Eval("SubCatName") %>' /><br />

            <ItemTemplate>
                Name:
                <asp:Label ID="Label1" runat="server" Text='<%# Eval("Name")%>' />
                <div id="Div1" style="clear: both" runat="server" Visible="<%# (Container.ItemIndex + 1) Mod 3 = 0%>"></div>
            </ItemTemplate>
  </ItemTemplate>

    String previousName = "";

    protected void MyDataList_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            Label Label1 = (Label)e.Item.FindControl("Label1");
            System.Data.DataRowView rowView = e.Item.DataItem as System.Data.DataRowView;
            string currentName = rowView["Name"].ToString();
            if (currentName == previousName)
            {
                Label1.Text = "";
            }
            else
            {
                Label1.Text = currentName;
            }
            previousName = currentName;
        }

    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM