using System;
using System.Drawing;
using System.Windows.Forms;
using System.Data;
using System.Collections.Generic;
using GrapeCity.Win.CalendarGrid;
namespace CalendarGridSampleCode
{
public class DataBindingDemo : Form
{
private GcCalendarGrid gcCalendarGrid = new GcCalendarGrid();
public DataBindingDemo()
{
this.Text = "DataBinding Demo";
this.Size = new Size(800, 400);
this.StartPosition = FormStartPosition.CenterScreen;
// Add GcCalendarGrid to form
this.gcCalendarGrid.Dock = DockStyle.Fill;
this.gcCalendarGrid.Protected = true;
this.Controls.Add(this.gcCalendarGrid);
this.Load += Form1_Load;
}
private void Form1_Load(object sender, EventArgs e)
{
InitializeDataSource();
// When data binding, the cells in template should be set DataField property to binding a specific column of data source.
this.gcCalendarGrid.Template = this.CreateDataBindingTamplate();
this.gcCalendarGrid.CalendarView = new CalendarListView();
this.gcCalendarGrid.AutoGenerateCellType = true;
this.gcCalendarGrid.ParseDateField += gcCalendarGrid_ParseDateField;
this.gcCalendarGrid.FormatDateField += gcCalendarGrid_FormatDateField;
this.gcCalendarGrid.DataSource = salesInfoDataSet.Tables["DailyRankList"];
this.gcCalendarGrid.DateField = "Date";//This name is from the DataColumn.Name which exists in DataTable "DailyRankList";
this.gcCalendarGrid.CellFormatting += gcCalendarGrid_CellFormatting;
this.gcCalendarGrid.CellParsing += gcCalendarGrid_CellParsing;
}
CalendarTemplate CreateDataBindingTamplate()
{
GrapeCity.Win.CalendarGrid.CalendarTemplate calendarTemplate1 = new GrapeCity.Win.CalendarGrid.CalendarTemplate();
calendarTemplate1.ColumnCount = 3;
calendarTemplate1.RowCount = 5;
calendarTemplate1.RowHeaderColumnCount = 0;
calendarTemplate1.ColumnHeaderRowCount = 0;
calendarTemplate1.Content.GetRow(0).Height = 28;
calendarTemplate1.Content.GetRow(1).Height = 28;
calendarTemplate1.Content.GetRow(2).Height = 28;
calendarTemplate1.Content.GetCell(0, 0).DateFormat = "d日";
calendarTemplate1.Content.GetCell(0, 0).DateFormatType = GrapeCity.Win.CalendarGrid.CalendarDateFormatType.DotNet;
calendarTemplate1.Content.GetCell(0, 0).RowSpan = 1;
calendarTemplate1.Content.GetCell(0, 0).ColumnSpan = 3;
calendarTemplate1.Content.GetCell(0, 0).CellStyle.Alignment = GrapeCity.Win.CalendarGrid.CalendarGridContentAlignment.MiddleCenter;
calendarTemplate1.Content.GetCell(1, 0).Value = "Name:";
calendarTemplate1.Content.GetCell(1, 0).Enabled = false;
calendarTemplate1.Content.GetCell(1, 1).DataField = "Name";
calendarTemplate1.Content.GetCell(1, 2).RowSpan = 2;
calendarTemplate1.Content.GetCell(1, 2).ColumnSpan = 1;
calendarTemplate1.Content.GetCell(1, 2).DataField = "Photo";
calendarTemplate1.Content.GetCell(2, 0).Value = "Gender:";
calendarTemplate1.Content.GetCell(2, 0).Enabled = false;
calendarTemplate1.Content.GetCell(2, 1).DataField = "Gender";
calendarTemplate1.Content.GetCell(2, 2).RowSpan = 1;
calendarTemplate1.Content.GetCell(2, 2).ColumnSpan = 1;
calendarTemplate1.Content.GetCell(3, 0).Value = "Amount of sales:";
calendarTemplate1.Content.GetCell(3, 0).Enabled = false;
calendarTemplate1.Content.GetCell(3, 1).RowSpan = 1;
calendarTemplate1.Content.GetCell(3, 1).ColumnSpan = 2;
calendarTemplate1.Content.GetCell(3, 1).DataField = "Amount";
calendarTemplate1.Content.GetCell(4, 0).Value = "Click To give Bonus:";
calendarTemplate1.Content.GetCell(4, 0).Enabled = false;
calendarTemplate1.Content.GetCell(4, 1).RowSpan = 1;
calendarTemplate1.Content.GetCell(4, 1).ColumnSpan = 2;
calendarTemplate1.Content.GetCell(4, 1).DataField = "GotBonus";
calendarTemplate1.Content.GetCell(4, 1).Locked = false;
return calendarTemplate1;
}
void gcCalendarGrid_ParseDateField(object sender, ParseDateFieldEventArgs e)
{
e.Date = DateTime.ParseExact(e.Value.ToString(), "yyyyMMdd", null);
}
void gcCalendarGrid_FormatDateField(object sender, FormatDateFieldEventArgs e)
{
e.Value = e.Date.ToString("yyyyMMdd");
}
void gcCalendarGrid_CellFormatting(object sender, CalendarCellFormattingEventArgs e)
{
if (e.CellPosition.Scope != CalendarTableScope.Content)
{
return;
}
if (e.CellPosition.RowIndex == 2 && e.CellPosition.ColumnIndex == 1)//Gender
{
if (object.Equals(e.Value, 0))
{
e.Value = "Male";
}
else if (object.Equals(e.Value, 1))
{
e.Value = "Female";
}
}
else if (e.CellPosition.RowIndex == 3 && e.CellPosition.ColumnIndex == 1)//Amount
{
if (e.Value != null)
{
e.Value = e.Value.ToString() + " USD";
e.CellStyle.BackColor = SystemColors.Info;
e.CellStyle.ForeColor = SystemColors.InfoText;
}
}
else if (e.CellPosition.RowIndex == 4 && e.CellPosition.ColumnIndex == 1)//Got Bonus
{
if (e.Value != null)
{
if (object.Equals(e.Value, 0))
{
e.Value = false;
}
else if (object.Equals(e.Value, 1))
{
e.Value = true;
}
}
}
}
void gcCalendarGrid_CellParsing(object sender, CalendarCellParsingEventArgs e)
{
if (e.CellPosition.Scope != CalendarTableScope.Content)
{
return;
}
if (e.CellPosition.RowIndex == 4 && e.CellPosition.ColumnIndex == 1)//Got Bonus
{
if (e.Value != null && e.Value.GetType() == typeof(bool) && e.DesiredType == typeof(int))
{
if (object.Equals(e.Value, true))
{
e.Value = 1;
}
else if (object.Equals(e.Value, false))
{
e.Value = 0;
}
}
}
}
#region Initialize DataSource
DataSet salesInfoDataSet = new DataSet();
private void InitializeDataSource()
{
// For demonstration purpose, I construct a data source by code.
// You can connect a database, and create data table or data set by ADO.net technique instead.
// For more detail information about ADO.net, please refer to MSDN
salesInfoDataSet.Tables.Add("DailyRankList");
salesInfoDataSet.Tables.Add("Ohters");
// Initialize BasicInfo table.
DataTable salesDailyRankList = salesInfoDataSet.Tables["DailyRankList"];
// Initialize columns
salesDailyRankList.Columns.Add("Date", typeof(string));
salesDailyRankList.Columns.Add("Name", typeof(string));
salesDailyRankList.Columns.Add("Gender", typeof(int));
salesDailyRankList.Columns.Add("Photo", typeof(Image));
salesDailyRankList.Columns.Add("Amount", typeof(double));
salesDailyRankList.Columns.Add("GotBonus", typeof(int));
// Initialize data.
salesDailyRankList.Rows.Add("20140101", "Jim", 0, SystemIcons.WinLogo.ToBitmap(), 485.45, 0);
salesDailyRankList.Rows.Add("20140102", "Alice", 1, SystemIcons.WinLogo.ToBitmap(), 685.45, 0);
salesDailyRankList.Rows.Add("20140103", "Tom", 0, SystemIcons.WinLogo.ToBitmap(), 485.45, 0);
salesDailyRankList.Rows.Add("20140104", "Lucy", 1, SystemIcons.WinLogo.ToBitmap(), 385.45, 0);
salesDailyRankList.Rows.Add("20140105", "Gaga", 1, SystemIcons.WinLogo.ToBitmap(), 985.45, 0);
salesDailyRankList.Rows.Add("20140106", "Ted", 0, SystemIcons.WinLogo.ToBitmap(), 586.45, 0);
salesDailyRankList.Rows.Add("20140107", "Bob", 0, SystemIcons.WinLogo.ToBitmap(), 489.45, 0);
salesDailyRankList.Rows.Add("20140108", "Lily", 1, SystemIcons.WinLogo.ToBitmap(), 585.45, 0);
salesDailyRankList.Rows.Add("20140109", "Susan", 1, SystemIcons.WinLogo.ToBitmap(), 475.45, 0);
}
#endregion
[STAThreadAttribute()]
public static void Main()
{
Application.EnableVisualStyles();
Application.Run(new DataBindingDemo());
}
}
}
Imports System.Drawing
Imports System.Windows.Forms
Imports System.Data
Imports System.Collections.Generic
Imports GrapeCity.Win.CalendarGrid
Namespace CalendarGridSampleCode
Public Class DataBindingDemo
Inherits Form
Private gcCalendarGrid As New GcCalendarGrid()
Public Sub New()
Me.Text = "DataBinding Demo"
Me.Size = New Size(800, 400)
Me.StartPosition = FormStartPosition.CenterScreen
' Add GcCalendarGrid to form
Me.gcCalendarGrid.Dock = DockStyle.Fill
Me.gcCalendarGrid.[Protected] = True
Me.Controls.Add(Me.gcCalendarGrid)
AddHandler Me.Load, AddressOf Form1_Load
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs)
InitializeDataSource()
' When data binding, the cells in template should be set DataField property to binding a specific column of data source.
Me.gcCalendarGrid.Template = Me.CreateDataBindingTamplate()
Me.gcCalendarGrid.CalendarView = New CalendarListView()
Me.gcCalendarGrid.AutoGenerateCellType = True
AddHandler Me.gcCalendarGrid.ParseDateField, AddressOf gcCalendarGrid_ParseDateField
AddHandler Me.gcCalendarGrid.FormatDateField, AddressOf gcCalendarGrid_FormatDateField
Me.gcCalendarGrid.DataSource = salesInfoDataSet.Tables("DailyRankList")
Me.gcCalendarGrid.DateField = "Date"
'This name is from the DataColumn.Name which exists in DataTable "DailyRankList";
AddHandler Me.gcCalendarGrid.CellFormatting, AddressOf gcCalendarGrid_CellFormatting
AddHandler Me.gcCalendarGrid.CellParsing, AddressOf gcCalendarGrid_CellParsing
End Sub
Private Function CreateDataBindingTamplate() As CalendarTemplate
Dim calendarTemplate1 As New GrapeCity.Win.CalendarGrid.CalendarTemplate()
calendarTemplate1.ColumnCount = 3
calendarTemplate1.RowCount = 5
calendarTemplate1.RowHeaderColumnCount = 0
calendarTemplate1.ColumnHeaderRowCount = 0
calendarTemplate1.Content.GetRow(0).Height = 28
calendarTemplate1.Content.GetRow(1).Height = 28
calendarTemplate1.Content.GetRow(2).Height = 28
calendarTemplate1.Content.GetCell(0, 0).DateFormat = "d日"
calendarTemplate1.Content.GetCell(0, 0).DateFormatType = GrapeCity.Win.CalendarGrid.CalendarDateFormatType.DotNet
calendarTemplate1.Content.GetCell(0, 0).RowSpan = 1
calendarTemplate1.Content.GetCell(0, 0).ColumnSpan = 3
calendarTemplate1.Content.GetCell(0, 0).CellStyle.Alignment = GrapeCity.Win.CalendarGrid.CalendarGridContentAlignment.MiddleCenter
calendarTemplate1.Content.GetCell(1, 0).Value = "Name:"
calendarTemplate1.Content.GetCell(1, 0).Enabled = False
calendarTemplate1.Content.GetCell(1, 1).DataField = "Name"
calendarTemplate1.Content.GetCell(1, 2).RowSpan = 2
calendarTemplate1.Content.GetCell(1, 2).ColumnSpan = 1
calendarTemplate1.Content.GetCell(1, 2).DataField = "Photo"
calendarTemplate1.Content.GetCell(2, 0).Value = "Gender:"
calendarTemplate1.Content.GetCell(2, 0).Enabled = False
calendarTemplate1.Content.GetCell(2, 1).DataField = "Gender"
calendarTemplate1.Content.GetCell(2, 2).RowSpan = 1
calendarTemplate1.Content.GetCell(2, 2).ColumnSpan = 1
calendarTemplate1.Content.GetCell(3, 0).Value = "Amount of sales:"
calendarTemplate1.Content.GetCell(3, 0).Enabled = False
calendarTemplate1.Content.GetCell(3, 1).RowSpan = 1
calendarTemplate1.Content.GetCell(3, 1).ColumnSpan = 2
calendarTemplate1.Content.GetCell(3, 1).DataField = "Amount"
calendarTemplate1.Content.GetCell(4, 0).Value = "Click To give Bonus:"
calendarTemplate1.Content.GetCell(4, 0).Enabled = False
calendarTemplate1.Content.GetCell(4, 1).RowSpan = 1
calendarTemplate1.Content.GetCell(4, 1).ColumnSpan = 2
calendarTemplate1.Content.GetCell(4, 1).DataField = "GotBonus"
calendarTemplate1.Content.GetCell(4, 1).Locked = False
Return calendarTemplate1
End Function
Private Sub gcCalendarGrid_ParseDateField(sender As Object, e As ParseDateFieldEventArgs)
e.[Date] = DateTime.ParseExact(e.Value.ToString(), "yyyyMMdd", Nothing)
End Sub
Private Sub gcCalendarGrid_FormatDateField(sender As Object, e As FormatDateFieldEventArgs)
e.Value = e.Date.ToString("yyyyMMdd")
End Sub
Private Sub gcCalendarGrid_CellFormatting(sender As Object, e As CalendarCellFormattingEventArgs)
If e.CellPosition.Scope <> CalendarTableScope.Content Then
Return
End If
If e.CellPosition.RowIndex = 2 AndAlso e.CellPosition.ColumnIndex = 1 Then
'Gender
If Object.Equals(e.Value, 0) Then
e.Value = "Male"
ElseIf Object.Equals(e.Value, 1) Then
e.Value = "Female"
End If
ElseIf e.CellPosition.RowIndex = 3 AndAlso e.CellPosition.ColumnIndex = 1 Then
'Amount
If e.Value IsNot Nothing Then
e.Value = e.Value.ToString() + " USD"
e.CellStyle.BackColor = SystemColors.Info
e.CellStyle.ForeColor = SystemColors.InfoText
End If
ElseIf e.CellPosition.RowIndex = 4 AndAlso e.CellPosition.ColumnIndex = 1 Then
'Got Bonus
If e.Value IsNot Nothing Then
If Object.Equals(e.Value, 0) Then
e.Value = False
ElseIf Object.Equals(e.Value, 1) Then
e.Value = True
End If
End If
End If
End Sub
Private Sub gcCalendarGrid_CellParsing(sender As Object, e As CalendarCellParsingEventArgs)
If e.CellPosition.Scope <> CalendarTableScope.Content Then
Return
End If
If e.CellPosition.RowIndex = 4 AndAlso e.CellPosition.ColumnIndex = 1 Then
'Got Bonus
If e.Value IsNot Nothing AndAlso e.Value.[GetType]().Equals(GetType(Boolean)) AndAlso e.DesiredType.Equals(GetType(Integer)) Then
If Object.Equals(e.Value, True) Then
e.Value = 1
ElseIf Object.Equals(e.Value, False) Then
e.Value = 0
End If
End If
End If
End Sub
#Region "Initialize DataSource"
Private salesInfoDataSet As New DataSet()
Private Sub InitializeDataSource()
' For demonstration purpose, I construct a data source by code.
' You can connect a database, and create data table or data set by ADO.net technique instead.
' For more detail information about ADO.net, please refer to MSDN
salesInfoDataSet.Tables.Add("DailyRankList")
salesInfoDataSet.Tables.Add("Ohters")
' Initialize BasicInfo table.
Dim salesDailyRankList As DataTable = salesInfoDataSet.Tables("DailyRankList")
' Initialize columns
salesDailyRankList.Columns.Add("Date", GetType(String))
salesDailyRankList.Columns.Add("Name", GetType(String))
salesDailyRankList.Columns.Add("Gender", GetType(Integer))
salesDailyRankList.Columns.Add("Photo", GetType(Image))
salesDailyRankList.Columns.Add("Amount", GetType(Double))
salesDailyRankList.Columns.Add("GotBonus", GetType(Integer))
' Initialize data.
salesDailyRankList.Rows.Add("20140101", "Jim", 0, SystemIcons.WinLogo.ToBitmap(), 485.45, 0)
salesDailyRankList.Rows.Add("20140102", "Alice", 1, SystemIcons.WinLogo.ToBitmap(), 685.45, 0)
salesDailyRankList.Rows.Add("20140103", "Tom", 0, SystemIcons.WinLogo.ToBitmap(), 485.45, 0)
salesDailyRankList.Rows.Add("20140104", "Lucy", 1, SystemIcons.WinLogo.ToBitmap(), 385.45, 0)
salesDailyRankList.Rows.Add("20140105", "Gaga", 1, SystemIcons.WinLogo.ToBitmap(), 985.45, 0)
salesDailyRankList.Rows.Add("20140106", "Ted", 0, SystemIcons.WinLogo.ToBitmap(), 586.45, 0)
salesDailyRankList.Rows.Add("20140107", "Bob", 0, SystemIcons.WinLogo.ToBitmap(), 489.45, 0)
salesDailyRankList.Rows.Add("20140108", "Lily", 1, SystemIcons.WinLogo.ToBitmap(), 585.45, 0)
salesDailyRankList.Rows.Add("20140109", "Susan", 1, SystemIcons.WinLogo.ToBitmap(), 475.45, 0)
End Sub
#End Region
<STAThreadAttribute> _
Public Shared Sub Main()
Application.EnableVisualStyles()
Application.Run(New DataBindingDemo())
End Sub
End Class
End Namespace