private void Form1_Load(object sender, EventArgs e)
{
// グループ化を許可します
fpSpread1.ActiveSheet.AllowGroup = true;
// データセットを作成します
DataTable DT = new DataTable();
DataSet DS = new DataSet();
DT = DS.Tables.Add("SampleTable");
DT.Columns.AddRange(new DataColumn[] { new DataColumn("ID", Type.GetType("System.Int32")), new DataColumn("テキスト", Type.GetType("System.String")), new DataColumn("値", Type.GetType("System.Int32")) });
DT.Rows.Add(new object[] { 10, "Text-10", 100 });
DT.Rows.Add(new object[] { 10, "Text-10", 200 });
DT.Rows.Add(new object[] { 10, "Text-10", 300 });
DT.Rows.Add(new object[] { 10, "Text-20", 400 });
DT.Rows.Add(new object[] { 10, "Text-20", 500 });
DT.Rows.Add(new object[] { 20, "Text-30", 600 });
DT.Rows.Add(new object[] { 20, "Text-30", 700 });
// データセットを接続します
fpSpread1.DataSource = DS;
// 1列目でグループ化を行います
FarPoint.Win.Spread.Model.GroupDataModel gm = new FarPoint.Win.Spread.Model.GroupDataModel(fpSpread1.ActiveSheet.Models.Data);
FarPoint.Win.Spread.SortInfo[] sort = new FarPoint.Win.Spread.SortInfo[1];
sort[0] = new FarPoint.Win.Spread.SortInfo(0, true);
gm.Group(sort);
fpSpread1.ActiveSheet.Models.Data = gm;
foreach (FarPoint.Win.Spread.Model.Group g in gm.Groups)
{
ProcessGroup(fpSpread1.ActiveSheet, gm, g);
}
fpSpread1.Grouped += fpSpread1_Grouped;
}
void fpSpread1_Grouped(object sender, EventArgs e)
{
// 最上位グループの取得
FarPoint.Win.Spread.SheetView sheet = ((FarPoint.Win.Spread.FpSpread)sender).ActiveSheet;
FarPoint.Win.Spread.Model.GroupDataModel gm = (FarPoint.Win.Spread.Model.GroupDataModel)sheet.Models.Data;
foreach (FarPoint.Win.Spread.Model.Group g in gm.Groups)
{
ProcessGroup(sheet, gm, g);
}
}
private void ProcessGroup(FarPoint.Win.Spread.SheetView sheet, FarPoint.Win.Spread.Model.GroupDataModel gm, FarPoint.Win.Spread.Model.Group g)
{
// グループの縮小表示
g.Expanded = false;
// 第3列の値の取得()
FarPoint.Win.Spread.Model.DefaultSheetDataModel dm = (FarPoint.Win.Spread.Model.DefaultSheetDataModel)gm.TargetModel;
double sumValue = 0;
for (int i = 0; i <= g.Rows.Count - 1; i++)
{
sumValue += (int)dm.GetValue((int)g.Rows[i], 2);
}
// 合計と平均値の表示
g.Text = sheet.Columns[g.Column].Label + ": ";
g.Text += dm.GetValue((int)g.Rows[0], g.Column).ToString();
g.Text += " SUM = " + sumValue.ToString("###,##0.##");
g.Text += " AVE = " + (sumValue / g.Rows.Count).ToString("###,##0.00");
}
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
' グループ化を許可します
FpSpread1.ActiveSheet.AllowGroup = True
' データセットを作成します
Dim DT As New DataTable
Dim DS As New DataSet
DT = DS.Tables.Add("SampleTable")
DT.Columns.AddRange(New DataColumn() { _
New DataColumn("ID", Type.GetType("System.Int32")), _
New DataColumn("テキスト", Type.GetType("System.String")), _
New DataColumn("値", Type.GetType("System.Int32")) _
})
DT.Rows.Add(New Object() {10, "Text-10", 100})
DT.Rows.Add(New Object() {10, "Text-10", 200})
DT.Rows.Add(New Object() {10, "Text-10", 300})
DT.Rows.Add(New Object() {10, "Text-20", 400})
DT.Rows.Add(New Object() {10, "Text-20", 500})
DT.Rows.Add(New Object() {20, "Text-30", 600})
DT.Rows.Add(New Object() {20, "Text-30", 700})
' データセットを接続します
FpSpread1.DataSource = DS
' 1列目でグループ化を行います
Dim gm As New FarPoint.Win.Spread.Model.GroupDataModel(FpSpread1.ActiveSheet.Models.Data)
Dim sort(0) As FarPoint.Win.Spread.SortInfo
sort(0) = New FarPoint.Win.Spread.SortInfo(0, True)
gm.Group(sort)
FpSpread1.ActiveSheet.Models.Data = gm
For Each g As FarPoint.Win.Spread.Model.Group In gm.Groups
ProcessGroup(FpSpread1.ActiveSheet, gm, g)
Next
End Sub
Private Sub FpSpread1_Grouped(sender As Object, e As EventArgs) Handles FpSpread1.Grouped
' 最上位グループの取得
Dim sheet As FarPoint.Win.Spread.SheetView = CType(sender, FarPoint.Win.Spread.FpSpread).ActiveSheet
Dim gm As FarPoint.Win.Spread.Model.GroupDataModel = sheet.Models.Data
For Each g As FarPoint.Win.Spread.Model.Group In gm.Groups
ProcessGroup(sheet, gm, g)
Next
End Sub
Private Sub ProcessGroup(ByVal sheet As FarPoint.Win.Spread.SheetView, ByVal gm As FarPoint.Win.Spread.Model.GroupDataModel, ByVal g As FarPoint.Win.Spread.Model.Group)
' グループの縮小表示
g.Expanded = False
' 第3列の値の取得()
Dim dm As FarPoint.Win.Spread.Model.DefaultSheetDataModel = CType(gm.TargetModel, FarPoint.Win.Spread.Model.DefaultSheetDataModel)
Dim sumValue As Double
For i As Integer = 0 To g.Rows.Count - 1
sumValue += dm.GetValue(g.Rows(i), 2)
Next
' 合計と平均値の表示
g.Text = sheet.Columns(g.Column).Label + ": "
g.Text += dm.GetValue(g.Rows(0), g.Column).ToString()
g.Text += " SUM = " + sumValue.ToString("###,##0.##")
g.Text += " AVE = " + (sumValue / g.Rows.Count).ToString("###,##0.00")
End Sub