PowerTools MultiRow for Windows Forms 8.0J
Calculation プロパティ
使用例 

SummaryCellでどのような計算を実行するかを取得または設定します。
構文
Public Property Calculation As ICalculation
public ICalculation Calculation {get; set;}

プロパティ値

実行する計算を表すICalculation値。既定値はnull 参照 (Visual Basicでは Nothing)です。
解説
定義済みのExpressionまたはMathStatisticsをこのプロパティに指定して計算を実行できます。また、ICalculationインタフェースを実装して計算をカスタマイズし、その計算結果をこのセルに表示することもできます。
使用例
次のサンプルコードは、集計セルの計算ロジックをカスタマイズする方法を示します。このサンプルコードは、SummaryCellクラスに示されている詳細なコード例の一部を抜粋したものです。
private SummaryCell CreateSubTotalSummaryCell()
{
    // Calculation the sub total base on 'Price' and 'Count' in same row.
    SummaryCell summaryCell = new SummaryCell();
    summaryCell.Name = "SubTotal";
    summaryCell.Calculation = new Expression("Price * Count");
    summaryCell.Style.Format = "C";
    summaryCell.Style.BackColor = Color.Wheat;
    return summaryCell;
}
private SummaryCell CreateTotalSummaryCell()
{
    // Calculate sum of sub-total in all rows.
    SummaryCell summaryCell = new SummaryCell();
    summaryCell.Name = "Total";
    summaryCell.Calculation = new MathStatistics(StatisticsType.Sum, "SubTotal", true);
    summaryCell.Style.Format = "C";
    summaryCell.Style.BackColor = Color.Wheat;
    return summaryCell;
}
private SummaryCell CreateCustomSummaryCell()
{
    SummaryCell summaryCell = new SummaryCell();
    // Custom calculation logic.
    summaryCell.Calculation = new PercentageCalculation();
    summaryCell.Style.Format = "#0.00%";
    summaryCell.Style.BackColor = Color.Wheat;
    return summaryCell;
}

class PercentageCalculation : ICalculation
{
    public object Calculate(CalculationContext context)
    {
        // calculate the percentage base on current section's sub-total in total.
        object subTotalValue = context.GcMultiRow[context.SectionIndex, "SubTotal"].Value;
        object totalValue = context.GcMultiRow.ColumnFooters[0]["Total"].Value;

        if (object.Equals(totalValue, 0m))
        {
            // context.ErrorInfo = "Total is 0.";
            return "Total is 0";
        }

        return (decimal)subTotalValue / (decimal)totalValue;
    }

    public object Clone()
    {
        // If you and new property in the calculation, make sure the property is cloned in this method.
        return new PercentageCalculation();
    }
}
Private Function CreateSubTotalSummaryCell() As SummaryCell
    ' Calculation the sub total base on 'Price' and 'Count' in same row.
    Dim summaryCell As New SummaryCell()
    summaryCell.Name = "SubTotal"
    summaryCell.Calculation = New Expression("Price * Count")
    summaryCell.Style.Format = "C"
    summaryCell.Style.BackColor = Color.Wheat
    Return summaryCell
End Function
Private Function CreateTotalSummaryCell() As SummaryCell
    ' Calculate sum of sub-total in all rows.
    Dim summaryCell As New SummaryCell()
    summaryCell.Name = "Total"
    summaryCell.Calculation = New MathStatistics(StatisticsType.Sum, "SubTotal", True)
    summaryCell.Style.Format = "C"
    summaryCell.Style.BackColor = Color.Wheat
    Return summaryCell
End Function
Private Function CreateCustomSummaryCell() As SummaryCell
    Dim summaryCell As New SummaryCell()
    ' Custom calculation logic.
    summaryCell.Calculation = New PercentageCalculation()
    summaryCell.Style.Format = "#0.00%"
    summaryCell.Style.BackColor = Color.Wheat
    Return summaryCell
End Function

Private Class PercentageCalculation
    Implements ICalculation
    Public Function Calculate(ByVal context As CalculationContext) As Object Implements ICalculation.Calculate
        ' calculate the percentage base on current section's sub-total in total.
        Dim subTotalValue As Object = context.GcMultiRow(context.SectionIndex, "SubTotal").Value
        Dim totalValue As Object = context.GcMultiRow.ColumnFooters(0)("Total").Value

        If Object.Equals(totalValue, 0D) Then
            ' context.ErrorInfo = "Total is 0.";
            Return "Total is 0"
        End If

        Return DirectCast(subTotalValue, Decimal) / DirectCast(totalValue, Decimal)
    End Function

    Public Function Clone() As Object Implements ICloneable.Clone
        ' If you and new property in the calculation, make sure the property is cloned in this method.
        Return New PercentageCalculation()
    End Function
End Class
参照

SummaryCell クラス
SummaryCell メンバ
Expression クラス
MathStatistics クラス

 

 


© 2008-2015 GrapeCity inc. All rights reserved.