如何使用自定义摘要获得多行?

时间:2019-05-15 15:49:23

标签: c# winforms devexpress

我可以在组合框中选择一个来获得和。但我想打印每一项的总和。

选择一个项目并输入一个值后,将添加所有项目的输入值。

我该如何解决?

就像下面的图片一样 enter image description here

private void gridView_CellValueChanged(object sender, CellValueChangedEventArgs e)
{
    GridView view = sender as GridView;
    exchangeModel = new ExchangeModel();
    exchangeModel.CurrencyName = view.GetFocusedRowCellValue("CurrencyName").ToString();
    exchangeModel.Amount = Convert.ToInt32(view.GetFocusedRowCellValue("Amount").ToString());
}

private void gridView_CustomSummaryCalculate(object sender, CustomSummaryEventArgs e)
{
    if (e.SummaryProcess == CustomSummaryProcess.Start)
    {
        dictionary = new Dictionary<string, List<decimal>>();
    }

    if (e.SummaryProcess == CustomSummaryProcess.Calculate)
    {
        string currencyName = e.GetValue("CurrencyName").ToString();
        List<decimal> list;

        if (!dictionary.TryGetValue(currencyName, out list))
        {
            list = new List<decimal>();
            dictionary.Add(currencyName, list);
        }

        list.Add(Convert.ToInt32(e.GetValue("Amount")));
    }

    if (e.SummaryProcess == CustomSummaryProcess.Finalize)
    {
        e.TotalValue = CalculateTotal();
    }
}

private object CalculateTotal()
{
    decimal sum = 0;
    IEnumerator enumerator = dictionary.GetEnumerator();
    enumerator.Reset();

    while (enumerator.MoveNext())
    {
        KeyValuePair<string, List<decimal>> current = ((KeyValuePair<string, List<decimal>>)enumerator.Current);

        for (int i = 0; i < current.Value.Count; i++)
        {
            sum += current.Value[i];
        }
    }

    return sum;
}

1 个答案:

答案 0 :(得分:0)

我建议您使用类似于GridColumn.Summary属性说明的 Example 部分中说明的方法,向列中添加一些自定义摘要。之后,根据 CustomSummaryCalculate e.Item 参数识别每个摘要,并将所需的值分配给e.TotalValue。

另请参见:
Summaries