gridcontrol 中怎样自定义totalsummary 计算过程

从blog.org的我的原博客中迁入

发表时间:2006-9-14 20:39:07

从devexpress官网查得:

A better way to implement a frequently used custom summary (Weighted Averages example)

Article ID: A2346 ; Product Group: .NET ; Product: XtraGrid ; Version(s): 2, 3, 6.x ; Modified On: 13 Sep 2006

Description

I need to implement weighted average summaries for a lot of the grids in my application. I know that this can be accomplished by handling the CustomSummaryCalculate event. However, the formula is quite complex and the event handler is large and so it's difficult to maintain it. Will you make the weighted average summary a built-in function of the XtraGrid?

Solution

Grid summaries are calculated for a single data column. Weighted averages depend upon the values in two columns. It may be difficult for us to redesign summation algorithms for multi-column formulas.

We can suggest a solution so you can easily include a custom summary function in all grids. You can move the summary formula out of the CustomSummaryCalculate event handler, so that a result is calculated in a separate function, which can be shared by several grids.

[C#]


using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid.Views.Grid;

public static object GetWeightedAverage(GridView view, string weightField, string valueField) {
 const decimal undefined = 0;
 if(view == null) return undefined;
 GridColumn weightCol = view.Columns[weightField];
 GridColumn valueCol = view.Columns[valueField];
 if(weightCol == null || valueCol == null) return undefined;
 
 try {
  decimal totalWeight = 0, totalValue = 0;
  for(int i = 0; i < view.DataRowCount; i++) {
   if(view.IsNewItemRow(i)) continue;
   object temp;
   decimal weight, val;
   temp = view.GetRowCellValue(i, weightCol);
   weight = (temp == DBNull.Value || temp == null) ? 0 : Convert.ToDecimal(temp);
   temp = view.GetRowCellValue(i, valueCol);
   val = (temp == DBNull.Value || temp == null) ? 0 : Convert.ToDecimal(temp);

   totalWeight += weight;
   totalValue += weight * val;
  }
  if(totalWeight == 0) return undefined;
  return totalValue / totalWeight;
 }
 catch {
  return undefined;
 }
}  
You still need to handle the CustomSummaryCalculate event, but just check that the e.SummaryProcess is equal to CustomSummaryProcess.Finalize and assign the summary result to the e.TotalValue parameter.

[C#]


private void gridView1_CustomSummaryCalculate(object sender,
DevExpress.Data.CustomSummaryEventArgs e) {
 if(e.IsTotalSummary && e.SummaryProcess == CustomSummaryProcess.Finalize) {
  GridView view = sender as GridView;
  if(e.Item == view.Columns["UnitPrice"].SummaryItem) {
   e.TotalValue = CustomSummaryHelper.GetWeightedAverage(view, "Quantity", "UnitPrice");
  }
 }  
=========================================================
Summary Values; groups

I'm trying to calculate the average of the sum of two columns in each group
and display the value in another column in the groupsummary.  How do I do
this?   I have set the field to type 'custom' and am trying to use the
customSummaryCalculate event to make this work.  I'm stumpped.  For each
record, I'm calculating a percentage, let's say column 'z', from two
columns, let's say column 'x' and column 'y'.  I'd like to be able to
calculate a summary avg for column 'z'.  Of course taking the avg of the
percentages is not accurate.   I need to be able to take the sum of x
divided by the sum of y to get an accurate total for column z for each
summarygroup.  The total summary value for the column I can get but not the
group value.  What I have listed below is the code the makes the TOTAL
summary value correct, but not the group value.  Any help would be greatly
appreciated.  Thanks.

Nick.

Private Sub GridView1_CustomSummaryCalculate(ByVal sender As Object, ByVal e
As DevExpress.XtraGrid.CustomSummaryEventArgs) Handles
GridView1.CustomSummaryCalculate

Try

With GridView1

Dim y As Integer = CDec(.Columns("numLblReq").SummaryItem.SummaryValue)

Dim x As Integer = CInt(.Columns("numLblReq").SummaryItem.SummaryValue) -
CInt(.Columns("numFail").SummaryItem.SummaryValue)

Dim n As Decimal

If y <> 0 Then

n = x / y

.Columns("successRate").SummaryItem.DisplayFormat = "Total AVG=" &
n.ToString("p")

End If

End With

Catch

End Try

End Sub

 

=================================

得到gridsummaryitem的值

Here the GridSummaryItem.DisplayFormat property is used for demonstration purposes only. To learn more about summary value formats, read the Format Summary Values topic.

To obtain summary values the GridView.GetGroupSummaryValues method is used.

C# Copy Code
private void gridView1_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e) {
   GridView currentView = sender as GridView;         
   // The group row's handle.
   int groupRowHandle;         
   if(e.FocusedRowHandle > 0) groupRowHandle = currentView.GetParentRowHandle(e.FocusedRowHandle);
   else groupRowHandle = e.FocusedRowHandle;
   if(!currentView.IsGroupRow(groupRowHandle)) return;

   // Get group summary values.
   Hashtable ht = currentView.GetGroupSummaryValues(groupRowHandle);
   Text = "";
   // Iterate through group summaries.
   foreach(DictionaryEntry entry in ht) {            
      GridGroupSummaryItem sumItem = entry.Key as GridGroupSummaryItem;
      object val = entry.Value;
      // Get the summry item's Tag property and summary value.
      Text += sumItem.Tag.ToString() + ": " + val.ToString() + "/t";            
   }
}
======================================
*************************
运行时动态建立groupsummay items

The second item can be customized in the same manner. See the Summaries Overview topic for details on how the summaries can be customized.

To add and customize group summary items at runtime, the following code can be used:

C# Copy Code
using DevExpress.XtraGrid;
//...
GridView view = gridView1;
// Create and setup the first summary item.
GridGroupSummaryItem item = new GridGroupSummaryItem();
item.FieldName = "ProductID";
item.SummaryType = DevExpress.Data.SummaryItemType.Count;
item.DisplayFormat = "Count = {0}";
item.Tag = "Count by ProductID";
// Add the summary item to the collection
view.GroupSummary.Add(item);
// Create and setup the second summary item
GridGroupSummaryItem item1 = new GridGroupSummaryItem();
item1.FieldName = "UnitPrice";
item1.SummaryType = DevExpress.Data.SummaryItemType.Sum;
item1.DisplayFormat = "Sum = {0:c2}";
item1.ShowInGroupColumnFooter = view.Columns["UnitPrice"];
item1.Tag = "Sum by UnitPrice";
// Add the summary item to the collection
view.GroupSummary.Add(item1);

你可能感兴趣的:(gridcontrol 中怎样自定义totalsummary 计算过程)