gpt4 book ai didi

asp.net - 可变中继器列

转载 作者:行者123 更新时间:2023-12-04 14:52:48 25 4
gpt4 key购买 nike

我有一个要绑定(bind)到中继器的对象数据源。
问题是,我无法弄清楚如何显示具有可变数量行的可变数量的列。

例如:

我拥有的数据集的结构是这样的。对象数据源是 List<item> .

item {
string name;
List<itemdata> data;
}

itemdata {
DateTime year;
double amount;
}

所以基本上我想做一张 table
      |  year  |  year  |  year  |  year
name | amount | amount | amount | amount
name | amount | amount | amount | amount
name | amount | amount | amount | amount
name | amount | amount | amount | amount

项目的数量是可变的,以及项目包含的 itemdata 的数量。

希望有人能指出我正确的方向。

谢谢

最佳答案

您的问题的解决方案将需要三个不同的中继器,其中一个嵌套在另一个中。从这样的标记开始。

  <table>
<tr class="headerRow">
<td> &nbsp;</td>
<asp:Repeater ID="rptYearHeader" runat="server" OnItemDataBound="rptYearHeader_ItemDataBound">
<ItemTemplate>
<td class="header"><asp:Literal ID="litYear" runat="server"></asp:Literal></td>
</ItemTemplate>
</asp:Repeater>
</tr>
<asp:Repeater ID="rptName" runat="server" ItemDataBound="rptName_ItemDataBound">
<ItemTemplate>
<tr>
<td><asp:Literal ID="litName" runat="server"></asp:Literal></td>
<asp:Repeater ID="rptAmounts" runat="server" OnItemDataBound="rptAmounts_ItemDataBound">
<ItemTemplate>
<td><asp:Literal ID="litAmount" runat="server"></asp:Literal></td>
</ItemTemplate>
</asp:Repeater>
</tr>
</ItemTemplate>
</asp:Repeater>
</table>

绑定(bind)到这个可能有点棘手。这个想法是,首先我们绑定(bind)标题行 - 然后我们绑定(bind)数据行和列。您将希望通过使用 OnItemDataBound 事件的代码来处理数据绑定(bind),以便您可以将嵌套中继器与必要的数据连接起来。

首先,我们将标题行与 Years 绑定(bind)。您需要隔离数据源中存在的一组唯一年份,并将其保存在私有(private)变量中。稍后您将需要在其他转发器的数据绑定(bind)期间访问它。这将作为标题行的数据源,为每年创建一个单元格/列。
List<DateTime> _Years =  dataSource.SelectMany(x => x.data).GroupBy(y => y.Year);
rptYear.DataSource = _Years;
rptYear.DataBind();

现在,您需要将名称转发器与您的原始数据源绑定(bind)。就像是
rptName.DataSource = dataSource;
rptName.DataBind();

这将为列表中的每个项目创建一行。

在此转发器的 OnItemDataBound 事件期间,您需要将嵌套转发器绑定(bind)到会计年度列表 - 我们的 _Years 变量中的每个会计年度一个 - 使用当前行数据项中的任何适用数据。这有点棘手,但我会尝试解释:
protected void rptName_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
// get the data item being bound
item currentItem = e.Item.DataItem as item;

// bind the item's name to the literal
//...
//

// get a list of amounts to bind to the nested repeater
// because we cant be sure that every item has amount for all years
// we create a list that we know has all years and plug in the items
// data accordingly.

List<double> amounts = new List<double>();
for (int i = 0; i < _Years.Count; i++)
{
// check whether the current item has data for the year
dataItem di = currentItem.data.Where(d => d.Year == _Years[i]).FirstOrDefault();

if(di == null)
{
// the year did not exist, so we add an amount of 0
amounts.Add(0);
}
else
{
// the year did exist, so we add that year's amount
amounts.Add(di.amount);
}
}

// we now have a list of amounts for all possible years, with 0 filling in
// where the item did not have a value for that year

// bind this to the nested repeater
rptAmounts.DataSource = amounts;
rptAmounts.DataBind();

}

祝你好运。

之前,我不得不使用多个嵌套中继器来实现小计和总计行。我开始在睡梦中看到嵌套的中继器。

关于asp.net - 可变中继器列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2090378/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com