可变中继列 [英] Variable repeater columns

查看:123
本文介绍了可变中继列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我要绑定到一个中继一个ObjectDataSource。
问题是,我不知道如何处理行数量可变显示的列数量可变的。

I have an objectdatasource which i want to bind to a repeater. the problem is, I cannot work out how to display a variable amount of columns with a variable amount of rows.

例如:

该数据集我的结构是这样的。 ObjectDataSource控件是一个列表与LT;项目方式>

The dataset I have is structured like this. The objectdatasource is a List<item>.

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

itemdata {
  DateTime year;
  double amount;
}

所以基本上我想打一个表

so basically i want to make a 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的数量的项包含

The number of items are variable, as well as the number of itemdata that the item contains.

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

Hope someone can point me in the right direction.

感谢

推荐答案

解决您的问题将需要三种不同的转发器,其中一个嵌套在另一个。像这样的标记开始。

The solution to your problem will require three different repeaters, one of which is nested inside another. Begin with the markup like this.

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

绑定到这可能是一个有点棘手。我们的想法是,首先我们结合标题行 - 然后我们绑定下来的数据行和整个列。你将要处理的数据背后,通过使用OnItemDataBound事件code绑定,这样就可以提供必要的数据线了嵌套的中继器。

Binding to this can be a little tricky. The idea is, first we bind the header row - then we bind down the data rows and across the columns. You will want to handle the data binding through code behind using the OnItemDataBound event so that you can wire up the nested repeater with the necessary data.

首先,我们的标题行绑定年。您需要隔离的独特年present集合在你的数据源,并保存在一个私有变量。您将需要的数据后,其他中继器的结合过程中访问它。这将作为标题行中的数据源,创造每年为一个单元格/列。

First we bind the header row with Years. You need to isolate a collection of unique years present in your datasource and keep it in a private variable. You will need to access it during the data binding of the other repeaters later. This will serve as the data source for the header row, creating one cell/column for each year.

List<DateTime> _Years =  dataSource.SelectMany(x => x.data).GroupBy(y => y.Year);
rptYear.DataSource = _Years;
rptYear.DataBind();

现在,您需要将名称中继与原始数据源绑定。类似

Now, you need to bind the Name repeater with your original data source. Something like

rptName.DataSource = dataSource;
rptName.DataBind();

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

This will create one row for each item in your list.

在OnItemDataBound事件此中继,则需要嵌套中继器绑定到会计年度的列表 - 每个每个会计年度之一,我们_Years变量 - 从当前行的数据项目的任何适用的数据。这变得有点棘手,但我会试着解释:

During the OnItemDataBound event for this repeater, you will need to bind the nested repeater to a list of fiscal years - one per each fiscal year in our _Years variable - with any applicable data from the current row's data item. This gets a little tricky, but I'll try to explain:

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

}

祝你好运。

我曾与前小计和总计行多重嵌套中继器来完成这件事。我开始在我睡觉看到嵌套的中继器。

I have had to pull this off with multiple nested repeaters for sub-total and grand total rows before. I started seeing nested repeaters in my sleep.

这篇关于可变中继列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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