PowerTools MultiRow for Windows Forms 8.0J
SummaryCell クラス
メンバ  使用例 

数値計算または統計計算の結果を表示するCellを表します。
構文
Public Class SummaryCell 
   Inherits LabelCell
public class SummaryCell : LabelCell 
解説
このCellは、単純な数値計算や統計計算を組み込む場合に役立ちます。定義済みの計算方法を使用する場合は、CalculationプロパティにMathStatisticsインスタンスを設定します。こうすると、すべてのRowの指定したセルを基にMathStatistics.StatisticsTypeで指定した種類の計算を実行して、値を求めることができます。四則演算式を自分で記述する場合は、CalculationプロパティをExpressionインスタンスに設定します。こうすると、その式の結果が計算されます。カスタム計算を行う場合は、ICalculationインタフェースを実装し、そのインスタンスをSummaryCellCalculationプロパティに設定します。
使用例
次のサンプルコードは、集計セルを使用して値を自動的に計算する方法を示します。
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Data;

namespace GrapeCity.Win.MultiRow.SampleCode
{
    public class SummaryCellDemo : Form
    {
        private GcMultiRow gcMultiRow1 = new GcMultiRow();

        private Label descriptionLable = new Label();

        public SummaryCellDemo()
        {
            this.Text = "SummaryCell Demo";
            this.Size = new Size(550, 300);

            // Add MultiRow to form
            this.gcMultiRow1.Dock = DockStyle.Fill;
            this.Controls.Add(this.gcMultiRow1);

            descriptionLable.Height = 40;
            descriptionLable.BackColor = SystemColors.Info;
            descriptionLable.Dock = DockStyle.Bottom;
            descriptionLable.Text = "'Total', 'SubTotal' and 'Percentage' are SummaryCell. " +
                                    "SummaryCell can calculate cell value base on other cells' value. " +
                                    "Try to update price or add new record and watch what happened";
            this.Controls.Add(descriptionLable);

            this.Load += new EventHandler(Form1_Load);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // create a template with SummaryCells.
            TextBoxCell nameTextBox = new TextBoxCell();

            NumericUpDownCell priceNumericUpDown = new NumericUpDownCell();
            priceNumericUpDown.ShowSpinButton = CellButtonVisibility.NotShown;
            priceNumericUpDown.DecimalPlaces = 2;
            priceNumericUpDown.Name = "Price";

            NumericUpDownCell countNumericUpDown = new NumericUpDownCell();
            countNumericUpDown.ShowSpinButton = CellButtonVisibility.NotShown;
            countNumericUpDown.Name = "Count";

            SummaryCell subTotalSummaryCell = CreateSubTotalSummaryCell();

            SummaryCell percentSummaryCell = CreateCustomSummaryCell();

            Cell[] cells = new Cell[] { nameTextBox, priceNumericUpDown, countNumericUpDown, subTotalSummaryCell, percentSummaryCell };

            Template template1 = Template.CreateGridTemplate(cells);

            ColumnFooterSection columnFooter = new ColumnFooterSection();
            columnFooter.Height = 21;
            template1.ColumnFooters.Add(columnFooter);

            SummaryCell totalSummaryCell = CreateTotalSummaryCell();
            totalSummaryCell.Location = subTotalSummaryCell.Location;
            columnFooter.Cells.Add(totalSummaryCell);

            LabelCell totalLabelCell = new LabelCell();
            totalLabelCell.Value = "Total:";
            totalLabelCell.Style.Border = Border.Empty;
            totalLabelCell.Location = countNumericUpDown.Location;
            columnFooter.Cells.Add(totalLabelCell);

            this.gcMultiRow1.Template = template1;
            this.gcMultiRow1.ColumnHeaders[0][0].Value = "Name";
            this.gcMultiRow1.ColumnHeaders[0][1].Value = "Price";
            this.gcMultiRow1.ColumnHeaders[0][2].Value = "Count";
            this.gcMultiRow1.ColumnHeaders[0][3].Value = "Sub Total";
            this.gcMultiRow1.ColumnHeaders[0][4].Value = "Percentage";

            this.FillValue();
        }
        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 void FillValue()
        {
            gcMultiRow1.Rows.Add("Apple", 2.25, 150);
            gcMultiRow1.Rows.Add("Banana", 8.3, 200);
            gcMultiRow1.Rows.Add("Watermelon", 5, 500);
            gcMultiRow1.Rows.Add("Pear", 3.2 , 355);
            gcMultiRow1.Rows.Add("Strawberry", 7.7, 440);
        }

        [STAThreadAttribute()]
        public static void Main()
        {
            Application.EnableVisualStyles();
            Application.Run(new SummaryCellDemo());
        }
    }
}
Imports System
Imports System.Drawing
Imports System.Windows.Forms
Imports System.Data
Imports GrapeCity.Win.MultiRow

Public Class SummaryCellDemo
    Inherits Form
    Private gcMultiRow1 As New GcMultiRow()

    Private descriptionLable As New Label()

    Public Sub New()
        Me.Text = "SummaryCell Demo"
        Me.Size = New Size(550, 300)

        ' Add MultiRow to form
        Me.gcMultiRow1.Dock = DockStyle.Fill
        Me.Controls.Add(Me.gcMultiRow1)

        descriptionLable.Height = 40
        descriptionLable.BackColor = SystemColors.Info
        descriptionLable.Dock = DockStyle.Bottom
        descriptionLable.Text = "'Total', 'SubTotal' and 'Percentage' are SummaryCell. " + "SummaryCell can calculate cell value base on other cells' value. " + "Try to update price or add new record and watch what happened"
        Me.Controls.Add(descriptionLable)
    End Sub

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
        ' create a template with SummaryCells.
        Dim nameTextBox As New TextBoxCell()

        Dim priceNumericUpDown As New NumericUpDownCell()
        priceNumericUpDown.ShowSpinButton = CellButtonVisibility.NotShown
        priceNumericUpDown.DecimalPlaces = 2
        priceNumericUpDown.Name = "Price"

        Dim countNumericUpDown As New NumericUpDownCell()
        countNumericUpDown.ShowSpinButton = CellButtonVisibility.NotShown
        countNumericUpDown.Name = "Count"

        Dim subTotalSummaryCell As SummaryCell = CreateSubTotalSummaryCell()

        Dim percentSummaryCell As SummaryCell = CreateCustomSummaryCell()

        Dim cells As Cell() = New Cell() {nameTextBox, priceNumericUpDown, countNumericUpDown, subTotalSummaryCell, percentSummaryCell}

        Dim template1 As Template = Template.CreateGridTemplate(cells)

        Dim columnFooter As New ColumnFooterSection()
        columnFooter.Height = 21
        template1.ColumnFooters.Add(columnFooter)

        Dim totalSummaryCell As SummaryCell = CreateTotalSummaryCell()
        totalSummaryCell.Location = subTotalSummaryCell.Location
        columnFooter.Cells.Add(totalSummaryCell)

        Dim totalLabelCell As New LabelCell()
        totalLabelCell.Value = "Total:"
        totalLabelCell.Style.Border = Border.Empty
        totalLabelCell.Location = countNumericUpDown.Location
        columnFooter.Cells.Add(totalLabelCell)

        Me.gcMultiRow1.Template = template1
        Me.gcMultiRow1.ColumnHeaders(0)(0).Value = "Name"
        Me.gcMultiRow1.ColumnHeaders(0)(1).Value = "Price"
        Me.gcMultiRow1.ColumnHeaders(0)(2).Value = "Count"
        Me.gcMultiRow1.ColumnHeaders(0)(3).Value = "Sub Total"
        Me.gcMultiRow1.ColumnHeaders(0)(4).Value = "Percentage"

        Me.FillValue()
    End Sub
    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
    Private Sub FillValue()
        gcMultiRow1.Rows.Add("Apple", 2.25, 150)
        gcMultiRow1.Rows.Add("Banana", 8.3, 200)
        gcMultiRow1.Rows.Add("Watermelon", 5, 500)
        gcMultiRow1.Rows.Add("Pear", 3.2, 355)
        gcMultiRow1.Rows.Add("Strawberry", 7.7, 440)
    End Sub

    <STAThreadAttribute()> _
    Public Shared Sub Main()
        Application.EnableVisualStyles()
        Application.Run(New SummaryCellDemo())
    End Sub
End Class
継承階層

System.Object
   System.MarshalByRefObject
      System.ComponentModel.Component
         GrapeCity.Win.MultiRow.Cell
            GrapeCity.Win.MultiRow.LabelCell
               GrapeCity.Win.MultiRow.SummaryCell

参照

SummaryCell メンバ
GrapeCity.Win.MultiRow 名前空間
Cell クラス
ICalculation インターフェース
Expression クラス
MathStatistics クラス

 

 


© 2008-2015 GrapeCity inc. All rights reserved.