Gridview获得两个总和之间的差异

时间:2014-12-29 09:08:53

标签: c# vb.net devexpress xtragrid

我正在使用devexpress XtraGrid控件。我的问题如下:我想得到第一列和第二列的总和。我最终想要减去第二列总和的第一列的总和,并在页脚中显示网格......

Sum1Columns - Sum2Columns = balance

然后在数据网格控件上显示平衡 - 页脚(在1列下方)

dgvVIEW.Columns(1).Name = "PROMDUGU"
dgvVIEW.Columns(1).Caption = "1COLUMN"
dgvVIEW.Columns(1).Visible = True
dgvVIEW.Columns(1).DisplayFormat.FormatType = FormatType.Numeric
dgvVIEW.Columns(1).DisplayFormat.FormatString = "c2"
dgvVIEW.Columns(1).SummaryItem.SummaryType = DevExpress.Data.SummaryItemType.Custom
dgvVIEW.Columns(1).SummaryItem.SummaryType = DevExpress.Data.SummaryItemType.Sum
dgvVIEW.Columns(1).SummaryItem.DisplayFormat = "SUM= {0:n2}"

dgvVIEW.Columns(2).Name = "PROMPOTR"
dgvVIEW.Columns(2).Caption = "2COLUMN"
dgvVIEW.Columns(2).Visible = True
dgvVIEW.Columns(2).DisplayFormat.FormatType = FormatType.Numeric
dgvVIEW.Columns(2).DisplayFormat.FormatString = "c2"
dgvVIEW.Columns(2).SummaryItem.SummaryType =  DevExpress.Data.SummaryItemType.Sum
dgvVIEW.Columns(2).SummaryItem.DisplayFormat = "Sum= {0:n2}"

1 个答案:

答案 0 :(得分:1)

将另一个摘要字段添加到第二列,并将其类型设置为自定义。

dgvVIEW.Columns(2).Summary.Add(new GridColumnSummaryItem(SummaryItemType.Custom, "customBalance", "Balance= {0:c2}"));

然后处理CustomSummaryCalculate事件。

private void dgvVIEW_CustomSummaryCalculate(object sender, CustomSummaryEventArgs e) {
    if (e.SummaryProcess == CustomSummaryProcess.Start) {
      this.sum1 = 0; // <--- class member !
      this.sum2 = 0; // <--- class member !
      return; 
    }

    if (e.SummaryProcess == CustomSummaryProcess.Calculate) {
      if (e.Item.FieldName == "PROMDUGU" {
        this.sum1 += Convert.ToDecimal(e.FieldValue);
        return;
      } 
      if (e.Item.FieldName == "PROMPOTR" {
        this.sum2 += Convert.ToDecimal(e.FieldValue);
        return;
      } 
      return; 
    }


    if (e.SummaryProcess == CustomSummaryProcess.Finalize && e.Item.FieldName == "customBalance") {
       e.TotalValue = sum1 - sum2;
    }
}