GrapeCity CalendarGrid for Windows Forms 3.0J
データベースとの連結

CalendarGridはADO.NETを使用してデータベースに接続できます。データソースの指定した日付フィールドと一致する日付に関連するデータをCalendarGridのセルに読み出せます。

注意

  • データソースに同一の日付が複数存在する場合、先頭のレコードが読み込まれます。


日付型のフィールドからの読み出し

データソースに日付型のフィールドが存在するときGcCalendarGridの次のプロパティにデータソースの情報を指定すると、GcCalendarGridはデータソースの1レコードを1日の情報として読み込みます。

プロパティ データソース

GcCalendarGrid.DataSource

対象のデータセット

GcCalendarGrid.DataMember

対象のテーブル名

GcCalendarGrid.DataField

テーブルに含まれる日付型のフィールドの名前

読み込まれたレコードの情報は、1日に含まれる各セルに表示できます。CalendarCell.DataFieldプロパティを使ってどのセルにどのフィールドの情報を表示するかを指定できます。

次のコードは、サンプルのデータソースを作成しこれを指定したセルに読み込むようにGcCalendarGridコントロールを定義します。

Imports GrapeCity.Win.CalendarGrid

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Dim _dataSet As DataSet = Nothing
    Dim table As New DataTable("MyTable")
    table.Columns.Add("Date", GetType(DateTime))
    table.Columns.Add("Field1", GetType(String))
    table.Columns.Add("Field2", GetType(Integer))

    table.BeginLoadData()
    For i As Integer = 1 To DateTime.DaysInMonth(DateTime.Today.Year, DateTime.Today.Month)
        table.Rows.Add(New Object() {New DateTime(DateTime.Today.Year, DateTime.Today.Month, i), "Test " + i.ToString(), i})
    Next
    table.EndLoadData()

    _dataSet = New DataSet("MyDataSet")
    _dataSet.Tables.Add(table)

    GcCalendarGrid1.DataSource = _dataSet
    GcCalendarGrid1.DataMember = "MyTable"
    GcCalendarGrid1.DateField = "Date"

    Dim template As CalendarTemplate = GcCalendarGrid1.Template
    template.Content.Rows(1).Cells(0).DataField = "Field1"
    template.Content.Rows(2).Cells(0).DataField = "Field2"
End Sub
using GrapeCity.Win.CalendarGrid;

private void Form1_Load(object sender, EventArgs e)
{
    DataSet _dataSet = null;
    var table = new DataTable("MyTable");
    table.Columns.Add("Date", typeof(DateTime));
    table.Columns.Add("Field1", typeof(string));
    table.Columns.Add("Field2", typeof(int));

    table.BeginLoadData();
    for (int i = 0; i < DateTime.DaysInMonth(DateTime.Today.Year, DateTime.Today.Month); i++)
    {
        table.Rows.Add(new object[] { new DateTime(DateTime.Today.Year, DateTime.Today.Month, i + 1), "Test " + i.ToString(), i });
    }
    table.EndLoadData();

    _dataSet = new DataSet("MyDataSet");
    _dataSet.Tables.Add(table);

    gcCalendarGrid1.DataSource = _dataSet;
    gcCalendarGrid1.DataMember = "MyTable";
    gcCalendarGrid1.DateField = "Date";

    CalendarTemplate template = gcCalendarGrid1.Template;
    template.Content.Rows[1].Cells[0].DataField = "Field1";
    template.Content.Rows[2].Cells[0].DataField = "Field2";
}


日付型以外のフィールドからの読み出し

データソースに格納されている日付が日付型(DateTime型)以外の場合、GcCalendarGrid.ParseDateFieldイベントを使用してGcCalendarGrid.DateFieldプロパティに指定されたフィールドの解析方法をカスタマイズして対応できます。

次のコードは"20140226"のように、日付型ではない形式で格納されている値を日付として読みだす例です。

Imports GrapeCity.Win.CalendarGrid

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Dim _dataSet As DataSet = Nothing
    Dim table As New DataTable("MyTable")
    table.Columns.Add("Date", GetType(String))
    table.Columns.Add("Field1", GetType(String))
    table.Columns.Add("Field2", GetType(Integer))

    table.BeginLoadData()
    For i As Integer = 1 To DateTime.DaysInMonth(DateTime.Today.Year, DateTime.Today.Month)
        Dim date1 As DateTime = New DateTime(DateTime.Today.Year, DateTime.Today.Month, i)
        table.Rows.Add(New Object() {date1.ToString("yyyyMMdd"), date1.ToString("yyyyMMdd"), i})
    Next
    table.EndLoadData()

    _dataSet = New DataSet("MyDataSet")
    _dataSet.Tables.Add(table)

    GcCalendarGrid1.DataSource = _dataSet
    GcCalendarGrid1.DataMember = "MyTable"
    GcCalendarGrid1.DateField = "Date"

    Dim template As CalendarTemplate = GcCalendarGrid1.Template
    template.Content.Rows(1).Cells(0).DataField = "Field1"
    template.Content.Rows(2).Cells(0).DataField = "Field2"
End Sub

Private Sub GcCalendarGrid1_ParseDateField(sender As Object, e As ParseDateFieldEventArgs) Handles GcCalendarGrid1.ParseDateField
    e.Date = DateTime.ParseExact(e.Value.ToString(), "yyyyMMdd", Nothing)
End Sub
using GrapeCity.Win.CalendarGrid;

private void Form1_Load(object sender, EventArgs e)
{
    DataSet _dataSet = null;
    gcCalendarGrid1.ParseDateField += gcCalendarGrid1_ParseDateField;

    var table = new DataTable("MyTable");
    table.Columns.Add("Date", typeof(string));
    table.Columns.Add("Field1", typeof(string));
    table.Columns.Add("Field2", typeof(int));

    table.BeginLoadData();
    for (int i = 0; i < DateTime.DaysInMonth(DateTime.Today.Year, DateTime.Today.Month); i++)
    {
        DateTime date1 = new DateTime(DateTime.Today.Year, DateTime.Today.Month, i + 1);
        table.Rows.Add(new object[] { date1.ToString("yyyyMMdd"), date1.ToString("yyyyMMdd"), i });
    }
    table.EndLoadData();

    _dataSet = new DataSet("MyDataSet");
    _dataSet.Tables.Add(table);

    gcCalendarGrid1.DataSource = _dataSet;
    gcCalendarGrid1.DataMember = "MyTable";
    gcCalendarGrid1.DateField = "Date";

    CalendarTemplate template = gcCalendarGrid1.Template;
    template.Content.Rows[1].Cells[0].DataField = "Field1";
    template.Content.Rows[2].Cells[0].DataField = "Field2";
}

private void gcCalendarGrid1_ParseDateField(object sender, ParseDateFieldEventArgs e)
{
    e.Date = DateTime.ParseExact(e.Value.ToString(), "yyyyMMdd", null);
}

 

データソースへの書き込み

データ連結している場合、CalendarGridのデータを変更してセルの編集モードを終了すると、データソースに変更したデータが書き込まれます。
また、CalendarGrid上でデータ変更した日付がデータソースに存在しない場合は、変更したデータとそのデータに関連する日付がデータソースに追加されます。

注意

  • データソースに存在しない日付をCalendarGrid上で変更した場合に、そのデータに関連する日付がデータソースに追加される動作はService Pack 1以降のバージョンで機能します。

次のサンプルコードは、CalendarGrid上でデータを変更してセルの編集状態が完了したときに、データソースに書き込まれたデータの内容を確認します。

Imports GrapeCity.Win.CalendarGrid

Dim _dataSet As DataSet = Nothing

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Dim table As New DataTable("MyTable")
    table.Columns.Add("Date", GetType(DateTime))
    table.Columns.Add("Field1", GetType(String))
    table.Columns.Add("Field2", GetType(Integer))

    table.BeginLoadData()
    For i As Integer = 1 To 3
        table.Rows.Add(New Object() {New DateTime(DateTime.Today.Year, DateTime.Today.Month, i), "Test " + i.ToString(), i})
    Next
    table.EndLoadData()

    _dataSet = New DataSet("MyDataSet")
    _dataSet.Tables.Add(table)

    GcCalendarGrid1.DataSource = _dataSet
    GcCalendarGrid1.DataMember = "MyTable"
    GcCalendarGrid1.DateField = "Date"

    Dim template As CalendarTemplate = GcCalendarGrid1.Template
    template.Content.Rows(1).Cells(0).DataField = "Field1"
    template.Content.Rows(2).Cells(0).DataField = "Field2"
End Sub

Private Sub GcCalendarGrid1_CellEndEdit(sender As Object, e As CalendarCellEndEditEventArgs) Handles GcCalendarGrid1.CellEndEdit

    ' データソースのデータを取得します。
    For i As Integer = 0 To _dataSet.Tables("MyTable").Rows.Count - 1
        Console.WriteLine(String.Format("Date:{0}, Field1:{1}, Field2:{2}", _
            _dataSet.Tables("MyTable").Rows(i)("Date").ToString(), _
            _dataSet.Tables("MyTable").Rows(i)("Field1").ToString(), _
            _dataSet.Tables("MyTable").Rows(i)("Field2").ToString()))
    Next
End Sub
using GrapeCity.Win.CalendarGrid;

DataSet _dataSet = null;

private void Form1_Load(object sender, EventArgs e)
{
    var table = new DataTable("MyTable");
    table.Columns.Add("Date", typeof(DateTime));
    table.Columns.Add("Field1", typeof(string));
    table.Columns.Add("Field2", typeof(int));

    table.BeginLoadData();
    for (int i = 0; i < 3; i++)
    {
        table.Rows.Add(new object[] { new DateTime(DateTime.Today.Year, DateTime.Today.Month, i + 1), "Test " + i.ToString(), i });
    }
    table.EndLoadData();

    _dataSet = new DataSet("MyDataSet");
    _dataSet.Tables.Add(table);

    gcCalendarGrid1.DataSource = _dataSet;
    gcCalendarGrid1.DataMember = "MyTable";
    gcCalendarGrid1.DateField = "Date";

    CalendarTemplate template = gcCalendarGrid1.Template;
    template.Content.Rows[1].Cells[0].DataField = "Field1";
    template.Content.Rows[2].Cells[0].DataField = "Field2";

    gcCalendarGrid1.CellEndEdit += gcCalendarGrid1_CellEndEdit;
}

private void gcCalendarGrid1_CellEndEdit(object sender, CalendarCellEndEditEventArgs e)
{

    // データソースのデータを取得します。
    for (int i = 0; i < _dataSet.Tables["MyTable"].Rows.Count; i++)
    {
        Console.WriteLine(string.Format("Date:{0}, Field1:{1}, Field2:{2}",
            _dataSet.Tables["MyTable"].Rows[i]["Date"].ToString(),
            _dataSet.Tables["MyTable"].Rows[i]["Field1"].ToString(),
            _dataSet.Tables["MyTable"].Rows[i]["Field2"].ToString()));
    }
}

操作手順例

上記のサンプルコードを次の手順で実行すると、「6日」と「テスト4」が関連したデータとしてデータソースに書き込まれます。

  1. サンプルコードを実行する。
  2. 2日の2行目のセルのデータを「Test 2」から「テスト2」に変更する
  3. セルの編集を終了すると、以下の結果が出力される。
     Date:2014/06/01 0:00:00, Field1:Test 1, Field2:1
     Date:2014/06/02 0:00:00, Field1:テスト2, Field2:2
     Date:2014/06/03 0:00:00, Field1:Test 3, Field2:3
  4. 6日の2行目のセルに「テスト4」を入力する。
  5. セルの編集を終了すると、以下の結果が出力される。
     Date:2014/06/01 0:00:00, Field1:Test 1, Field2:1
     Date:2014/06/02 0:00:00, Field1:テスト2, Field2:2
     Date:2014/06/03 0:00:00, Field1:Test 3, Field2:3
     Date:2014/06/06 0:00:00, Field1:テスト4, Field2: 
関連トピック

 

 


© 2008 GrapeCity inc. All rights reserved.