嵌套中继器-分组数据 [英] Nested repeaters - grouping data

查看:42
本文介绍了嵌套中继器-分组数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从ASP.NET开始,我想知道是否存在任何智能"方式以分组形式显示DataTable中的数据?
想象下面的例子:我从SQL获取数据到已分组并排序的数据表:

I'm beginning with ASP.NET and I'm wondering, if there is any "smart" way of displaying data from DataTable in groups?
Imagine following example: I get data from SQL to datatable grouped and sorted alredy:

protected void Page_Load(object sender, EventArgs e)
{
    DataTable dt = new DataTable();
    dt.Columns.Add("parent", typeof(string));
    dt.Columns.Add("child", typeof(string));
    dt.Rows.Add("AAA", "111");
    dt.Rows.Add("AAA", "222");
    dt.Rows.Add("AAA", "333");
    dt.Rows.Add("BBB", "444");
    dt.Rows.Add("BBB", "555");
    dt.Rows.Add("CCC", "666");
    dt.Rows.Add("CCC", "777");
    dt.Rows.Add("CCC", "888");

    repeaterParent.DataSource = dt;
    repeaterParent.DataBind();
}

,我想在下面的页面上显示它们:

and I want to display them on page like below:

我尝试为此使用嵌套中继器:

I've tried to use nested repeaters for that:

<asp:Repeater ID="repeaterParent" runat="server">
    <ItemTemplate>
        <b><%# DataBinder.Eval(Container.DataItem, "parent") %></b>
        <br /> 
        <asp:repeater id="repeaterChild" runat="server">
            <itemtemplate>
                    - <%# DataBinder.Eval(Container.DataItem, "child") %> <br />
            </itemtemplate>
        </asp:repeater>
    </ItemTemplate>
</asp:Repeater>

当然不起作用-结果如下-它既未分组也未显示子项:

Of course it doesn't work - the result is following - it's neither grouped nor a child is being displayed:

该怎么做-控件不必是重复器-它可以是列表视图,项目符号列表或类似的东西.

How can it be done - the control doesn't have to be repeater - it could be listview, bulleted list or something similar.

最好,如果有某种智能"方式-所以我只将数据表绑定到数据源,然后控件为我进行其余(分组)工作.我宁愿避免将数据源表拆分为两个不同的源,或者避免对每个父项进行SQL查询并使用ItemDataBound事件-只要有可能.如果不是,您可以向我展示任何方法吗?.NET版本:4.0

Best, if there's some "smart" way - so I just bind datatable to datasource, and the control makes the rest (grouping) for me. I would prefer to avoid splitting datasource table into two different sources or making SQL query for each parent item and playing with ItemDataBound event - as long as it's possible. If not - can you show me any way of doing that? .NET version: 4.0

最诚挚的问候,
马辛

Best regards,
Marcin

-
我找到了以下解决方案,但是,我真的不喜欢它,因为每个项目的源表过滤成本很高,我不确定它在大型源数据表中的表现如何.我将很高兴听到是否有更好的解决方案:

--
I've found following solution, however, I don't really like it because costly filtering source table for each item, I'm not sure how it will behave with large source datatable. I'll gladly hear if there's a better solution:

protected void repeaterParent_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    DataRowView drv = (DataRowView)e.Item.DataItem;
    string parent = drv["parent"].ToString();

    dt.DefaultView.RowFilter = string.Format("[parent]='{0}'", parent);
    DataTable dtChild =  dt.DefaultView.ToTable(true, "child");

    Repeater repeaterChild = (Repeater)e.Item.FindControl("repeaterChild");
    repeaterChild.DataSource = dtChild;
    repeaterChild.DataBind();

}

推荐答案

您处在正确的轨道上,但是您只有一个数据源,这是第一个转发器的输入.

You are on the right track, but ou have only one datasource and that is the input for the first repeater.

您需要向第二个(嵌套的)中继器添加第二个数据源.例如,一个函数返回第一个数据源中实际父级的子级.

You need to add a second datasource to the second (nested) repeater. For example a function that returns the childs of the actual parent in the first datasource.

ASP.NET中的嵌套中继器

这篇关于嵌套中继器-分组数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆