作成日: 2017/07/26 最終更新日: 2017/07/26
文書種別
使用方法
詳細
CalendarGrid for Windows Forms では、セルの値を自動で計算する機能を提供していません。
セルの値を計算するには、コーディングによる実装が必要になります。
以下は、CellValueChangedイベントで、2行目から5行目のセルの合計値を計算して、6行目のセルに結果を表示するサンプルコードです。
[Visual Basic]
Imports GrapeCity.Win.CalendarGrid
Imports InputManCell = GrapeCity.Win.CalendarGrid.InputMan
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
' セル型を設定します。
Dim GcNumberCellType As New InputManCell.CalendarGcNumberCellType
Dim LabelCellType As New CalendarLabelCellType()
' テンプレートを設定します。
Dim Template As New CalendarTemplate()
Template.RowCount = 6
Template.ColumnHeader.Rows(0).Cells(0).DateFormat = "{DayOfWeek}"
Template.ColumnHeader.Columns(0).Width = 100
Template.Content.Rows(0).Cells(0).DateFormat = "{MonthDay}"
Template.Content.Rows(0).Cells(0).CellStyleName = "defaultStyle"
Template.Content.Rows(1).Cells(0).Name = "numberCell1"
Template.Content.Rows(1).Cells(0).CellType = GcNumberCellType.Clone()
Template.Content.Rows(1).Cells(0).CellStyleName = "defaultStyle"
Template.Content.Rows(1).Cells(0).Value = 0
Template.Content.Rows(2).Cells(0).Name = "numberCell2"
Template.Content.Rows(2).Cells(0).CellType = GcNumberCellType.Clone()
Template.Content.Rows(2).Cells(0).CellStyleName = "defaultStyle"
Template.Content.Rows(2).Cells(0).Value = 0
Template.Content.Rows(3).Cells(0).Name = "numberCell3"
Template.Content.Rows(3).Cells(0).CellType = GcNumberCellType.Clone()
Template.Content.Rows(3).Cells(0).CellStyleName = "defaultStyle"
Template.Content.Rows(3).Cells(0).Value = 0
Template.Content.Rows(4).Cells(0).Name = "numberCell4"
Template.Content.Rows(4).Cells(0).CellType = GcNumberCellType.Clone()
Template.Content.Rows(4).Cells(0).CellStyleName = "defaultStyle"
Template.Content.Rows(4).Cells(0).Value = 0
Template.Content.Rows(5).Cells(0).Name = "resultCell"
Template.Content.Rows(5).Cells(0).CellType = LabelCellType.Clone()
Template.Content.Rows(5).Cells(0).CellStyleName = "defaultStyle"
Template.Content.Rows(5).Cells(0).Value = 0
GcCalendarGrid1.Template = Template
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim today As DateTime = DateTime.Today
' セルに値を設定します。
GcCalendarGrid1.Content(today).Rows(1).Cells(0).Value = 1
GcCalendarGrid1.Content(today).Rows(2).Cells(0).Value = 2
GcCalendarGrid1.Content(today).Rows(3).Cells(0).Value = 3
GcCalendarGrid1.Content(today).Rows(4).Cells(0).Value = 4
End Sub
Private Sub GcCalendarGrid1_CellValueChanged(sender As Object, e As CalendarCellEventArgs) Handles GcCalendarGrid1.CellValueChanged
If e.CellPosition.RowIndex = 5 Then
Return
End If
' 2行目から5行目の合計値を計算します。
Dim result As Integer = GcCalendarGrid1.Content(e.CellPosition.Date).Rows(1).Cells(0).Value +
GcCalendarGrid1.Content(e.CellPosition.Date).Rows(2).Cells(0).Value +
GcCalendarGrid1.Content(e.CellPosition.Date).Rows(3).Cells(0).Value +
GcCalendarGrid1.Content(e.CellPosition.Date).Rows(4).Cells(0).Value
' 計算結果を4行目のセルに設定します。
GcCalendarGrid1.Content(e.CellPosition.Date).Rows(5).Cells(0).Value = result
End Sub
[C#]
using GrapeCity.Win.CalendarGrid;
using InputManCell = GrapeCity.Win.CalendarGrid.InputMan;
private void Form1_Load(object sender, EventArgs e)
{
// セル型を設定します。
InputManCell.CalendarGcNumberCellType gcNumberCellType = new InputManCell.CalendarGcNumberCellType();
CalendarLabelCellType labelCellType = new CalendarLabelCellType();
// テンプレートを設定します。
CalendarTemplate template = new CalendarTemplate();
template.RowCount = 6;
template.ColumnHeader.Rows[0].Cells[0].DateFormat = "{DayOfWeek}";
template.ColumnHeader.Columns[0].Width = 100;
template.Content.Rows[0].Cells[0].DateFormat = "{MonthDay}";
template.Content.Rows[0].Cells[0].CellStyleName = "defaultStyle";
template.Content.Rows[1].Cells[0].Name = "numberCell1";
template.Content.Rows[1].Cells[0].CellType = gcNumberCellType.Clone();
template.Content.Rows[1].Cells[0].CellStyleName = "defaultStyle";
template.Content.Rows[1].Cells[0].Value = 0;
template.Content.Rows[2].Cells[0].Name = "numberCell2";
template.Content.Rows[2].Cells[0].CellType = gcNumberCellType.Clone();
template.Content.Rows[2].Cells[0].CellStyleName = "defaultStyle";
template.Content.Rows[2].Cells[0].Value = 0;
template.Content.Rows[3].Cells[0].Name = "numberCell3";
template.Content.Rows[3].Cells[0].CellType = gcNumberCellType.Clone();
template.Content.Rows[3].Cells[0].CellStyleName = "defaultStyle";
template.Content.Rows[3].Cells[0].Value = 0;
template.Content.Rows[4].Cells[0].Name = "numberCell4";
template.Content.Rows[4].Cells[0].CellType = gcNumberCellType.Clone();
template.Content.Rows[4].Cells[0].CellStyleName = "defaultStyle";
template.Content.Rows[4].Cells[0].Value = 0;
template.Content.Rows[5].Cells[0].Name = "resultCell";
template.Content.Rows[5].Cells[0].CellType = labelCellType.Clone();
template.Content.Rows[5].Cells[0].CellStyleName = "defaultStyle";
template.Content.Rows[5].Cells[0].Value = 0;
gcCalendarGrid1.Template = template;
button1.Click += button1_Click;
gcCalendarGrid1.CellValueChanged += gcCalendarGrid1_CellValueChanged;
}
private void button1_Click(object sender, EventArgs e)
{
DateTime today = DateTime.Today;
// セルに値を設定します。
gcCalendarGrid1.Content[today].Rows[1].Cells[0].Value = 1;
gcCalendarGrid1.Content[today].Rows[2].Cells[0].Value = 2;
gcCalendarGrid1.Content[today].Rows[3].Cells[0].Value = 3;
gcCalendarGrid1.Content[today].Rows[4].Cells[0].Value = 4;
}
private void gcCalendarGrid1_CellValueChanged(object sender, CalendarCellEventArgs e)
{
if (e.CellPosition.RowIndex == 5)
{
return;
}
// 2行目から5行目の合計値を計算します。
int result = int.Parse(gcCalendarGrid1.Content[e.CellPosition.Date].Rows[1].Cells[0].Value.ToString()) +
int.Parse(gcCalendarGrid1.Content[e.CellPosition.Date].Rows[2].Cells[0].Value.ToString()) +
int.Parse(gcCalendarGrid1.Content[e.CellPosition.Date].Rows[3].Cells[0].Value.ToString()) +
int.Parse(gcCalendarGrid1.Content[e.CellPosition.Date].Rows[4].Cells[0].Value.ToString());
// 計算結果を4行目のセルに設定します。
gcCalendarGrid1.Content[e.CellPosition.Date].Rows[5].Cells[0].Value = result;
}
セルの値を計算するには、コーディングによる実装が必要になります。
以下は、CellValueChangedイベントで、2行目から5行目のセルの合計値を計算して、6行目のセルに結果を表示するサンプルコードです。
[Visual Basic]
Imports GrapeCity.Win.CalendarGrid
Imports InputManCell = GrapeCity.Win.CalendarGrid.InputMan
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
' セル型を設定します。
Dim GcNumberCellType As New InputManCell.CalendarGcNumberCellType
Dim LabelCellType As New CalendarLabelCellType()
' テンプレートを設定します。
Dim Template As New CalendarTemplate()
Template.RowCount = 6
Template.ColumnHeader.Rows(0).Cells(0).DateFormat = "{DayOfWeek}"
Template.ColumnHeader.Columns(0).Width = 100
Template.Content.Rows(0).Cells(0).DateFormat = "{MonthDay}"
Template.Content.Rows(0).Cells(0).CellStyleName = "defaultStyle"
Template.Content.Rows(1).Cells(0).Name = "numberCell1"
Template.Content.Rows(1).Cells(0).CellType = GcNumberCellType.Clone()
Template.Content.Rows(1).Cells(0).CellStyleName = "defaultStyle"
Template.Content.Rows(1).Cells(0).Value = 0
Template.Content.Rows(2).Cells(0).Name = "numberCell2"
Template.Content.Rows(2).Cells(0).CellType = GcNumberCellType.Clone()
Template.Content.Rows(2).Cells(0).CellStyleName = "defaultStyle"
Template.Content.Rows(2).Cells(0).Value = 0
Template.Content.Rows(3).Cells(0).Name = "numberCell3"
Template.Content.Rows(3).Cells(0).CellType = GcNumberCellType.Clone()
Template.Content.Rows(3).Cells(0).CellStyleName = "defaultStyle"
Template.Content.Rows(3).Cells(0).Value = 0
Template.Content.Rows(4).Cells(0).Name = "numberCell4"
Template.Content.Rows(4).Cells(0).CellType = GcNumberCellType.Clone()
Template.Content.Rows(4).Cells(0).CellStyleName = "defaultStyle"
Template.Content.Rows(4).Cells(0).Value = 0
Template.Content.Rows(5).Cells(0).Name = "resultCell"
Template.Content.Rows(5).Cells(0).CellType = LabelCellType.Clone()
Template.Content.Rows(5).Cells(0).CellStyleName = "defaultStyle"
Template.Content.Rows(5).Cells(0).Value = 0
GcCalendarGrid1.Template = Template
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim today As DateTime = DateTime.Today
' セルに値を設定します。
GcCalendarGrid1.Content(today).Rows(1).Cells(0).Value = 1
GcCalendarGrid1.Content(today).Rows(2).Cells(0).Value = 2
GcCalendarGrid1.Content(today).Rows(3).Cells(0).Value = 3
GcCalendarGrid1.Content(today).Rows(4).Cells(0).Value = 4
End Sub
Private Sub GcCalendarGrid1_CellValueChanged(sender As Object, e As CalendarCellEventArgs) Handles GcCalendarGrid1.CellValueChanged
If e.CellPosition.RowIndex = 5 Then
Return
End If
' 2行目から5行目の合計値を計算します。
Dim result As Integer = GcCalendarGrid1.Content(e.CellPosition.Date).Rows(1).Cells(0).Value +
GcCalendarGrid1.Content(e.CellPosition.Date).Rows(2).Cells(0).Value +
GcCalendarGrid1.Content(e.CellPosition.Date).Rows(3).Cells(0).Value +
GcCalendarGrid1.Content(e.CellPosition.Date).Rows(4).Cells(0).Value
' 計算結果を4行目のセルに設定します。
GcCalendarGrid1.Content(e.CellPosition.Date).Rows(5).Cells(0).Value = result
End Sub
[C#]
using GrapeCity.Win.CalendarGrid;
using InputManCell = GrapeCity.Win.CalendarGrid.InputMan;
private void Form1_Load(object sender, EventArgs e)
{
// セル型を設定します。
InputManCell.CalendarGcNumberCellType gcNumberCellType = new InputManCell.CalendarGcNumberCellType();
CalendarLabelCellType labelCellType = new CalendarLabelCellType();
// テンプレートを設定します。
CalendarTemplate template = new CalendarTemplate();
template.RowCount = 6;
template.ColumnHeader.Rows[0].Cells[0].DateFormat = "{DayOfWeek}";
template.ColumnHeader.Columns[0].Width = 100;
template.Content.Rows[0].Cells[0].DateFormat = "{MonthDay}";
template.Content.Rows[0].Cells[0].CellStyleName = "defaultStyle";
template.Content.Rows[1].Cells[0].Name = "numberCell1";
template.Content.Rows[1].Cells[0].CellType = gcNumberCellType.Clone();
template.Content.Rows[1].Cells[0].CellStyleName = "defaultStyle";
template.Content.Rows[1].Cells[0].Value = 0;
template.Content.Rows[2].Cells[0].Name = "numberCell2";
template.Content.Rows[2].Cells[0].CellType = gcNumberCellType.Clone();
template.Content.Rows[2].Cells[0].CellStyleName = "defaultStyle";
template.Content.Rows[2].Cells[0].Value = 0;
template.Content.Rows[3].Cells[0].Name = "numberCell3";
template.Content.Rows[3].Cells[0].CellType = gcNumberCellType.Clone();
template.Content.Rows[3].Cells[0].CellStyleName = "defaultStyle";
template.Content.Rows[3].Cells[0].Value = 0;
template.Content.Rows[4].Cells[0].Name = "numberCell4";
template.Content.Rows[4].Cells[0].CellType = gcNumberCellType.Clone();
template.Content.Rows[4].Cells[0].CellStyleName = "defaultStyle";
template.Content.Rows[4].Cells[0].Value = 0;
template.Content.Rows[5].Cells[0].Name = "resultCell";
template.Content.Rows[5].Cells[0].CellType = labelCellType.Clone();
template.Content.Rows[5].Cells[0].CellStyleName = "defaultStyle";
template.Content.Rows[5].Cells[0].Value = 0;
gcCalendarGrid1.Template = template;
button1.Click += button1_Click;
gcCalendarGrid1.CellValueChanged += gcCalendarGrid1_CellValueChanged;
}
private void button1_Click(object sender, EventArgs e)
{
DateTime today = DateTime.Today;
// セルに値を設定します。
gcCalendarGrid1.Content[today].Rows[1].Cells[0].Value = 1;
gcCalendarGrid1.Content[today].Rows[2].Cells[0].Value = 2;
gcCalendarGrid1.Content[today].Rows[3].Cells[0].Value = 3;
gcCalendarGrid1.Content[today].Rows[4].Cells[0].Value = 4;
}
private void gcCalendarGrid1_CellValueChanged(object sender, CalendarCellEventArgs e)
{
if (e.CellPosition.RowIndex == 5)
{
return;
}
// 2行目から5行目の合計値を計算します。
int result = int.Parse(gcCalendarGrid1.Content[e.CellPosition.Date].Rows[1].Cells[0].Value.ToString()) +
int.Parse(gcCalendarGrid1.Content[e.CellPosition.Date].Rows[2].Cells[0].Value.ToString()) +
int.Parse(gcCalendarGrid1.Content[e.CellPosition.Date].Rows[3].Cells[0].Value.ToString()) +
int.Parse(gcCalendarGrid1.Content[e.CellPosition.Date].Rows[4].Cells[0].Value.ToString());
// 計算結果を4行目のセルに設定します。
gcCalendarGrid1.Content[e.CellPosition.Date].Rows[5].Cells[0].Value = result;
}
旧文書番号
40735