我在gridcontrol的页脚中有一个摘要字段。在gridcontrol中,第一列中有CheckButtons,供用户选择要处理的记录。我只需要汇总字段以汇总选定的行即可。现在,它汇总每一行。我如何才能将其汇总为选定的行?
您需要将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;
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句