我在网格控件的页脚中有一个摘要字段。在网格控件中,我在第一列上有检查按钮,供用户选择要处理的记录。我需要修复汇总字段,仅对选定的行进行求和。现在它对每一行求和。我怎样才能只计算所选行的总和?
最佳答案
您需要更改GridColumn.SummaryItem.SummaryType
属性至SummaryItemType.Custom
并使用GridView.CustomSummaryCalculate
事件来设置摘要的值。但是您无法在 GridView.CustomSummaryCalculate 事件中获取有关所选行的信息。这就是为什么您需要在 GridView.SelectionChanged
事件中计算总和,并在 GridView.CustomSummaryCalculate
事件中使用该总和。
这是示例:
private int _selectedSum;
private string _fieldName = "TOPLAM";
private void Form1_Load(object sender, EventArgs e)
{
var column = gridView1.Columns[_fieldName];
column.SummaryItem.SummaryType = SummaryItemType.Custom;
}
private void gridView1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var column = gridView1.Columns[_fieldName];
switch (e.Action)
{
case CollectionChangeAction.Add:
_selectedSum += (int)gridView1.GetRowCellValue(e.ControllerRow, column);
break;
case CollectionChangeAction.Remove:
_selectedSum -= (int)gridView1.GetRowCellValue(e.ControllerRow, column);
break;
case CollectionChangeAction.Refresh:
_selectedSum = 0;
foreach (var rowHandle in gridView1.GetSelectedRows())
_selectedSum += (int)gridView1.GetRowCellValue(rowHandle, column);
break;
}
gridView1.UpdateTotalSummary();
}
private void gridView1_CustomSummaryCalculate(object sender, CustomSummaryEventArgs e)
{
var item = e.Item as GridColumnSummaryItem;
if (item == null || item.FieldName != _fieldName)
return;
if (e.SummaryProcess == CustomSummaryProcess.Finalize)
e.TotalValue = _selectedSum;
}
关于c# - 如何向 devexpress gridcontrol 添加自定义摘要项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25686372/