DotNetBar Schedule 加载数据库数据

TaKaSa阅读(62517)

运行结果展示
数据库结构及数据
using DBUtility;
using DevComponents.DotNetBar.Schedule;
using DevComponents.Schedule.Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TestDotNetBarSchedule
{
    public partial class Form1 : Form
    {
        public CalendarModel model = new CalendarModel();
        public Form1()
        {
            InitializeComponent();
            //calendarView1.CalendarModel.AppointmentAdded += new AppointmentEventHandler(ModelAppointmentAdded);
            //calendarView1.CalendarModel.AppointmentRemoved += new AppointmentEventHandler(ModelAppointmentRemoved);
            //calendarView1.AppointmentViewChanged += new EventHandler<AppointmentViewChangedEventArgs>(AppointmentViewChanged);
           // calendarView1.CalendarModel = new CalendarModel(); 
        }

        #region Handled events

        /// <summary>
        /// Event sent when an appointment is added to the Model
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void ModelAppointmentAdded(object sender, AppointmentEventArgs e)
        {
            Console.WriteLine(@"{0} New Appointment Added. Appointment Subject: {1}",
                DateTime.Now, e.Appointment.Subject);
        }

        /// <summary>
        /// Event sent when an appointment is removed from the Model
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void ModelAppointmentRemoved(object sender, AppointmentEventArgs e)
        {
            Console.WriteLine(@"{0} Appointment Removed. Appointment Subject: {1}",
                DateTime.Now, e.Appointment.Subject);
        }

        /// <summary>
        /// Event sent when an appointment is either moved or resized
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void AppointmentViewChanged(object sender, AppointmentViewChangedEventArgs e)
        {
            AppointmentView view = e.CalendarItem as AppointmentView;

            if (view != null)
            {
                string sOperation = (e.eViewOperation == eViewOperation.AppointmentMove)
                    ? "Moved" : "Resized";

                Console.WriteLine(@"{0} Appointment {1}. Appointment Subject: {2}",
                    DateTime.Now, sOperation, view.Appointment.Subject);
            }
        }
        #endregion

        private void Form1_Load(object sender, EventArgs e)
        {
            calendarView1.DisplayedOwners.Add("TrueBeam");
            ////CalendarModel model = new CalendarModel();

            ////calendarView1.CalendarModel = model;
            ListAppointment();
           // AddSampleAppointments();

        }

        private void calendarView1_SelectedViewChanged(object sender, DevComponents.DotNetBar.Schedule.SelectedViewEventArgs e)
        {
            switch (e.NewValue)
            {
                case eCalendarView.Day:
                    btnDay.Checked = true;
                    break;

                case eCalendarView.Week:
                    btnWeek.Checked = true;
                    break;

                case eCalendarView.Month:
                    btnMonth.Checked = true;
                    break;

                   
            }
        }

        private void btnDay_Click(object sender, EventArgs e)
        {
            calendarView1.SelectedView = eCalendarView.Day;
        }

        private void btnWeek_Click(object sender, EventArgs e)
        {
            calendarView1.SelectedView = eCalendarView.Week;
        }

        private void btnMonth_Click(object sender, EventArgs e)
        {
            calendarView1.SelectedView = eCalendarView.Month;
        }
//加载数据库数据
        private void ListAppointment()
        {
            calendarView1.CalendarModel.Appointments.Clear();
            string sql = "select subject, starttime, endtime, categorycolor,ownerkey,timemarkedas,occurrences from appointment";
            SqlDataReader dr = SQLHelper.GetReader(sql);
            Appointment item;
            while (dr.Read())
            {
                item = new Appointment
                {
                    Subject = dr["subject"].ToString(),
                    StartTime = Convert.ToDateTime(dr["starttime"].ToString()),
                    EndTime = Convert.ToDateTime(dr["endtime"].ToString()),
                    CategoryColor = dr["categorycolor"].ToString(),
                    OwnerKey = dr["ownerkey"].ToString(),
                    TimeMarkedAs = dr["timemarkedas"].ToString()
                };
                item.Recurrence = new AppointmentRecurrence();
                item.Recurrence.RecurrenceType = eRecurrencePatternType.Daily;
                item.Recurrence.Daily.RepeatOnDaysOfWeek = eDailyRecurrenceRepeat.WeekDays;
                item.Recurrence.Daily.RepeatInterval = 1;
                item.Recurrence.RangeLimitType = eRecurrenceRangeLimitType.RangeNumberOfOccurrences;
                item.Recurrence.RangeNumberOfOccurrences = Convert.ToInt32(dr["occurrences"]);
  
                item.DisplayTemplate = item.StartTime.ToString("t") + "-" + item.EndTime.ToString("t") + " | " + item.Subject;//个性化显示文本
                calendarView1.CalendarModel.Appointments.Add(item);
               
            }
            
        }

        //private void AddSampleAppointments()
        //{
        //    // Recurring appointment

        //    Appointment appointment = new Appointment();
        //    appointment.Subject = "Recurring Appointment every 3rd week day";
        //    appointment.Description = "Custom description for this appointment";
        //    appointment.Tooltip = "This is a Custom tooltip for this appointment";
        //    appointment.CategoryColor = Appointment.CategoryOrange;
        //    appointment.StartTime = DateTime.Today.AddHours(8.00);
        //    appointment.EndTime = appointment.StartTime.AddMinutes(10);
        //    appointment.OwnerKey = "TrueBeam";
        //    // Set recurrence type to Daily
        //    appointment.Recurrence = new AppointmentRecurrence();
        //    appointment.Recurrence.RecurrenceType = eRecurrencePatternType.Daily;
        //    appointment.Recurrence.Daily.RepeatOnDaysOfWeek = eDailyRecurrenceRepeat.WeekDays;
        //    appointment.Recurrence.Daily.RepeatInterval = 1;
        //    //appointment.Recurrence.RangeLimitType = eRecurrenceRangeLimitType.RangeEndDate;
        //    appointment.Recurrence.RangeLimitType = eRecurrenceRangeLimitType.RangeNumberOfOccurrences;
        //    //appointment.Recurrence.RangeEndDate = DateTime.Today.AddDays(3);
        //    appointment.Recurrence.RangeNumberOfOccurrences = 25;
        //    // Add appointment to the model
        //    calendarView1.CalendarModel.Appointments.Add(appointment);
           

        //}
    }
}

DotNetBar Schedule的使用总结

TaKaSa阅读(3483)

一. 基本使用

// 第一步:创建模型
CalendarModel _Model = new CalendarModel();
// 第二步:创建一个新的约会并将它存储到模型中,约会将显示在模型中的各个视图中
Appointment appointment = new Appointment();
appointment.Subject = "Create Demo Application";//主题
appointment.StartTime = DateTime.Now;//开始时间
appointment.EndTime = appointment.StartTime.AddMinutes(45);//结束时间
appointment.CategoryColor = Appointment.CategoryGreen;//背景颜色
appointment.TimeMarkedAs = Appointment.TimerMarkerBusy;//标记颜色
//添加约会到模型中 
_Model.Appointments.Add(appointment);

二. Appointment约会的属性

  1. Subject 约会上显示的文本
  2. Description 约会的描述文字
  3. CategoryColor 约会的背景颜色、
  4. TimeMarkedAs 约会的时间标记
  5. StartTime 约会的开始时间
  6. EndTime 约会的结束时间
  7. OWnerKey 约会的所有者

三. 处理约会

约会存储在CalendarModel.Appointments集合中。但是,如果您正在使用定期约会,则在约会集合中只能找到原始约会实例。要获取某一天的所有约会, CalendarModel.GetDay  方法

以下代码获取2个时间内的所有约会:

DateTime start = new DateTime(2014, 05, 07);
DateTime end = start.AddDays(10);
DateTime d = start;
while (d <= end)
{
    DevComponents.Schedule.Model.Day day = calendarView1.CalendarModel.GetDay(d);
    foreach (Appointment item in day.Appointments)
    {
        Console.WriteLine("Appointment: {0}-{1} {2}", item.StartTime, item.EndTime, item.Subject);
    }
    d = d.AddDays(1);
}

四. 定期约会(Recurring Appointments)

WinForms日程控制提供了一个强大的定期约会引擎。支持以下定期类型:Daily:每天重复的约会Weekly:每周(对于每周或每周某些天重复发生的约会)Monthly:每月重复的约会Yearly :每年重复的约会要设置定期约会,首先需要将appointment.recurrence.recurrenceType设置为要使用的定期模式,即每天、每周、每月或每年。然后通过相应的属性设置重复模式属性,即:每日重复使用:约会。重复使用。每日每周重复使用:appointment.recurrence.weekly每月重复使用:appointment.recurrence.monthly每年重复使用:appointment.recurrence.yearly例如,要创建每日约会重复周期,即每隔3天重复一次约会,30天后结束,您可以将属性设置为:

Appointment ap = new Appointment(); 
// Set recurrence type to dail  
// 将定期类型设置为dail
ap.Recurrence.RecurrenceType = eRecurrencePatternType.Daily;
// Recurrence properties are changed then on respective object Daily, Weekly, Monthly, Yearly 
// 然后在各自的对象上更改定期属性,分别是每日、每周、每月、每年
ap.Recurrence.Daily.RepeatOnDaysOfWeek = eDailyRecurrenceRepeat.All;
// Repeate every 3 days 
// 每3天重复一次
ap.Recurrence.Daily.RepeatInterval = 3;
// End recurrence 30 days from today 
ap.Recurrence.RangeEndDate = DateTime.Today.AddDays(30);

添加一个25次的日程,代码如下:

 Appointment appointment = new Appointment();

            appointment.Subject = "Recurring Appointment every 3rd week day";
            appointment.Description = "Custom description for this appointment";
            appointment.Tooltip = "This is a Custom tooltip for this appointment";
            appointment.CategoryColor = Appointment.CategoryOrange;

            appointment.StartTime = DateTime.Today.AddHours(8.00);
            appointment.EndTime = appointment.StartTime.AddMinutes(10);

            // Set recurrence type to Daily

            appointment.Recurrence = new AppointmentRecurrence();

            appointment.Recurrence.RecurrenceType = eRecurrencePatternType.Daily;
            appointment.Recurrence.Daily.RepeatOnDaysOfWeek = eDailyRecurrenceRepeat.WeekDays;
            appointment.Recurrence.Daily.RepeatInterval = 1;
            //appointment.Recurrence.RangeLimitType = eRecurrenceRangeLimitType.RangeEndDate;
            appointment.Recurrence.RangeLimitType = eRecurrenceRangeLimitType.RangeNumberOfOccurrences;
            //appointment.Recurrence.RangeEndDate = DateTime.Today.AddDays(3);
            appointment.Recurrence.RangeNumberOfOccurrences = 25;
            // Add appointment to the model

            calendarView1.CalendarModel.Appointments.Add(appointment);

五. 切换视图、皮肤及转到今天的代码

//转到今天
 calendarView1.ShowDate(DateTime.Today);
//日视图
calendarView1.SelectedView = eCalendarView.Day;
//周视图
calendarView1.SelectedView = eCalendarView.Week;
//月视图
calendarView1.SelectedView = eCalendarView.Month;
//皮肤切换
 styleManager1.ManagerStyle = eStyle.Office2007Blue;