【问题标题】:Inject Rows into Silverlight DataGrid将行注入 Silverlight DataGrid
【发布时间】:2010-05-03 20:27:32
【问题描述】:

有人知道将行注入 Silverlight DataGrid 的方法吗?我需要一种在每 6 个条目后添加总计行的方法。总计是汇总行上方的所有行,而不仅仅是前六行。

我尝试过分组,但它只显示组的总计,并在实际行上方显示总计行。如果可能,我更愿意为此使用 DataGrid,但欢迎所有解决方案。

【问题讨论】:

    标签: silverlight datagrid


    【解决方案1】:

    好的,我有一个解决方案;但是,它包含一些时髦的数学功夫,所以我不确定它是否是最好的解决方案。但是,它确实适用于 DataGrid。

    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 是一个 ObservableCollection,Info 只是我创建的一个测试类。我确实非常彻底地评论了代码。从某种意义上说,它比您的要求更通用,您可以将 numRowsToSum 更改为任何值,并且它仍然可以工作。

    【讨论】:

    • 我希望找到一个不需要直接添加到绑定集合的解决方案,但我最终做了一些与此非常相似的事情。感谢您的帮助。
    猜你喜欢
    • 2011-04-21
    • 2010-12-20
    • 1970-01-01
    • 2012-06-28
    • 2011-05-15
    • 2010-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多