Variable repeater columns

后端 未结 2 788
北荒
北荒 2021-01-05 14:42

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

2条回答
  •  忘掉有多难
    2021-01-05 15:32

    I suggest you convert your data into a new data structure like this:

    name_data {  
      string name;
      int[] amounts;
    }
    

    You would then bind your repeater to a List(name_data>.

    To create this, first, iterate through item and data lists and keep a list (a List probably) of all the unique years you need to report on. Sort the resulting list so that the years are in order. Now, the indexes of the year list correspond to the column numbers in your output table. Next, iterate through the item list again, this time creating a new name_data object for each item object. The name_data constructor would look like this:

    public name_data(string name, int yearCount) {
      this.name = name;
      amounts = new int[yearCount];
    }
    

    The yearCount is the number of items in the year list.

    Finally, step through the data list for the current item, look up the year in the year list to get the index, then stick the amount value in the amount field in the corresponding amounts slot. Add your completed name_data to the List.

    Once you are done, you should be able to bind your name_data list to your repeater

提交回复
热议问题