代码之家  ›  专栏  ›  技术社区  ›  Stephan

将行插入Silverlight数据报

  •  2
  • Stephan  · 技术社区  · 14 年前

    有人知道如何将行插入到Silverlight数据报中吗?我需要一种方法在每6个条目后添加总计行。总计用于汇总行上方的所有行,而不仅仅是前六行。

    我尝试过分组,但这只显示组的合计,并显示实际行上方的合计行。如果可能的话,我希望使用一个数据报,但是所有的解决方案都是受欢迎的。

    1 回复  |  直到 10 年前
        1
  •  1
  •   BenMorel Manish Pradhan    10 年前

    好吧,我有一个解决方案给你,不过,里面有一些古怪的数学功夫,所以我不确定这是否是最好的解决方案。但是,它可以处理数据报。

    public void PopulateAndSum()
        {
    
            //Ugly Math Kung Fu starts here
            //Get the number of rows in the collection
            int rowCount = Items.Count;
    
            //Here you specify after how many rows should the summation occur
            int numRowsToSum = 6;
    
            //Now the idea is to loop through the collection the same number of times
            //As you would be inserting summation rows into the collection
            //To calculate the maximum number of times you should circulate you
            //divide through the number of rows and ceil the result. Make sure
            //divide by a double or at least cast one of them to a double so you do
            //not get integer division
            for (int i = 0; i < Math.Ceiling(((double)rowCount) / numRowsToSum); i++)
            {
                //Now you want to calculate the position where you need to insert the the new entry
                int index = 0;
    
                //Check whether you are still in the bounds of the array or whether you have actually reached the last element
                //in the array. This should always be the case if your collection contains a multiple of numRowsToSum
                if (numRowsToSum + i * (numRowsToSum) <= rowCount)
                {
                    //The index starts at numRowsToSum because you start the collection indexing at 0
                    //From there you jump the next index and add one to take into account that you inserted a new element
                    index = numRowsToSum + i*(numRowsToSum + 1);
                }
                else
                {
                    //If your collection contains a number of elements that are not a precise multiple of numRowsToSum
                    //then you have to have a special condition where you calculate the index for the last few elements
                    //Here you need to jump back to the previous index and add the number of elements left in the collection
                    //You also have to add 1 to take into account that you are adding an extra row.
                    index = numRowsToSum + (i - 1) * (numRowsToSum + 1) + 1 + rowCount % numRowsToSum;
                }
    
                //Now you sum all the rows before the index
    
                int sum = 0;
                for (int j = 0; j < index; j++)
                {
                    //If you want to add the previous totals to the sum comment the next part out
                    if ((j - numRowsToSum) % (numRowsToSum + 1) == 0)
                        continue;
    
                    sum += Items[j].Value;
                }
    
                //Now add the entry
                Items.Insert(index, new Info() { Name = "Total", Value = sum });
            }
        }
    

    Items 是一个可观察的集合 Info 只是我创建的一个测试类。我对代码做了很彻底的评论。从某种意义上说,它比您的需求更通用,您可以更改 numRowsToSum 任何价值,它应该仍然有效。