gridView4.OptionsView.ShowFooter = true;//启用显示页脚
//索引为1的列
gridView4.Columns[1].SummaryItem.DisplayFormat = "{0:0.##}";
gridView4.Columns[1].SummaryItem.FieldName = "TotalMoney";
gridView4.Columns[1].SummaryItem.SummaryType = DevExpress.Data.SummaryItemType.Sum;//求和
//索引为2的列
gridView4.Columns[2].SummaryItem.DisplayFormat = "{0:0.##}";
gridView4.Columns[2].SummaryItem.FieldName = "PayTotalMoney";
gridView4.Columns[2].SummaryItem.SummaryType = DevExpress.Data.SummaryItemType.Sum;//求和
//索引为3的列
gridView4.Columns[3].SummaryItem.DisplayFormat = "{0:0.##}";
gridView4.Columns[3].SummaryItem.FieldName = "UnPayMoney";
gridView4.Columns[3].SummaryItem.SummaryType = DevExpress.Data.SummaryItemType.Sum;//求和
gridView4.CustomDrawFooterCell += new FooterCellCustomDrawEventHandler(this.gridView4_CustomDrawFooterCell);//设置一下对齐方式,通过GridView的CustomDrawFooterCell 事件
//GridControl中的CustomDrawFooterCell事件
private void gridView4_CustomDrawFooterCell(object sender, DevExpress.XtraGrid.Views.Grid.FooterCellCustomDrawEventArgs e)
{
e.Appearance.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;
}
效果图如下:
如果不想汇总所有数据,而是根据选择汇总,则可以在GridControl中设置勾选框,汇总选中的行数据进行汇总:
设置勾选
Grid Designer - Views - OptionSelection-设置其中的MultiSelect为True,设置MultiSelectMode为CheckBoxRowSelect
选择表格的SelectionChanged事件,代码如下
private void gridView6_SelectionChanged(object sender, DevExpress.Data.SelectionChangedEventArgs e)
{
gridView6.OptionsView.ShowFooter = true;//启用显示页脚
willPay = 0;//清零数据
foreach (var item in gridView6.GetSelectedRows())//遍历选中行
{
//获取选中行的对象
IncoiceInfoSum incoice = (IncoiceInfoSum)gridView6.GetRow(item);
willPay += incoice.ThisPay;//数据累加
}
//数据显示
gridView6.Columns[6].SummaryItem.DisplayFormat = "付款汇总:" + willPay.ToString("0.##") + "";
gridView6.CustomDrawFooterCell += new FooterCellCustomDrawEventHandler(this.gridView6_CustomDrawFooterCell);//单元格居中显示
//刷新选中行
gridView6.RefreshRow(gridView6.FocusedRowHandle);
}
这样就可以实现自动汇总勾选行的数据,效果如下: