我在网格控件的页脚中有一个摘要字段。在网格控件中,我在第一列中有CheckButtons,供用户选择要处理的记录。我只需要将汇总字段修正为所选行的总和。现在它把每一行加起来。如何才能使它只与选定的行相加?

发布于 2014-09-08 05:57:19
您需要将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;
}https://stackoverflow.com/questions/25686372
复制相似问题