We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Reading and Writing schedule data from/to SQL

Hi,

I'm trying to build an app that manages a schedule on a desktop application. This application should be able to read and write to/from SQL database so that I can pull the data to a mobile application later on.

Are there any simple samples available that implements CRUD with the ScheduleControl on WinForms?



1 Reply

JP Jagadeesan Pichaimuthu Syncfusion Team April 25, 2019 11:38 AM UTC

Hi Marco,  

Thanks for using Syncfusion products.  

By default, Schedule control does not have the direct support to bind the data from DB. If you want to generate the appointment based on the DB data, you can create the appointment based on the DB data using StartTime, EndTime and Subject property in SimpleSheduleDataProvider. Please refer to the below code example,   
  
VB  
SimpleScheduleDataProvider dataProvider = new SimpleScheduleDataProvider(); 
dataProvider.MasterList = SimpleScheduleDataProvider.InitializeRandomData(); 
scheduleControl1.DataSource = dataProvider; 
public class SimpleScheduleDataProvider : ScheduleDataProvider 
{ 
 
    DataSet dset = new DataSet(); 
        static DataTable dt = new DataTable(); 
                /// <summary> 
                /// Default constructor. 
                /// </summary> 
                public SimpleScheduleDataProvider() 
                                : base() 
                { 
          dset.ReadXml(@"..\..\default.xml"); 
          dt = dset.Tables["MasterList"]; 
                } 
 
                 
 
                private SimpleScheduleAppointmentList masterList; 
 
                /// <summary> 
                /// Get or sets an IScheduleAppointmentList collection that holds the IScheduleAppointments.  
                /// </summary> 
                public SimpleScheduleAppointmentList MasterList 
                { 
                                get{return masterList;} 
                                set{masterList = value;} 
                } 
 
                #region random data 
 
    public static SimpleScheduleAppointmentList InitializeRandomData() 
    { 
        Random r = new Random(tc); 
 
        // set the number of sample items you want in this list. 
        int count = dt.Rows.Count;  
        SimpleScheduleAppointmentList masterList = new SimpleScheduleAppointmentList(); 
        for (int i = 0; i < count; ++i) 
        { 
            ScheduleAppointment item = masterList.NewScheduleAppointment() as ScheduleAppointment; 
            int dayOffSet = 30 - r.Next(60); 
            int hourOffSet = 24 - r.Next(48); 
            int len = 30 * (r.Next(4) + 1); 
            item.StartTime = DateTime.Now.AddDays(i);  
            item.EndTime = DateTime.Now.AddDays(i);            item.Subject = dt.Rows[i]["Subject"].ToString(); 
            item.Content = dt.Rows[i]["Content"].ToString(); 
            item.LabelValue = Convert.ToInt16(dt.Rows[i]["LabelValue"]); 
            item.LocationValue = dt.Rows[i]["LocationValue"].ToString(); 
            item.MarkerValue = (Convert.ToInt16(dt.Rows[i]["MarkerValue"])); 
            item.UniqueId = i; 
           item.Dirty = false; 
            masterList.Add(item); 
        } 
        masterList.SortStartTime(); 
        return masterList; 
} 
  
To perform the CRUD operation in ScheduleControl to your binding source, you could perform the operation using ItemChanged event. Please refer the following code example. 
 
Edit 
this.scheduleControl1.ItemChanged += ScheduleControl1_ItemChanged; 
private void ScheduleControl1_ItemChanged(object sender, ScheduleAppointmentEventArgs e) 
{ 
    if (e.Action == ItemAction.Edit) 
    { 
        int id = e.CurrentItem.UniqueID; 
        var dr = SimpleScheduleDataProvider.dt.Rows.Cast<DataRow>().First(x => x[10].Equals(e.CurrentItem.UniqueID.ToString())); 
        int index = SimpleScheduleDataProvider.dt.Rows.IndexOf(dr as DataRow); 
        if (index > -1) 
        { 
            dr[0] = e.CurrentItem.StartTime; 
            dr[1] = e.CurrentItem.EndTime; 
            dr[2] = e.CurrentItem.Subject; 
            dr[3] = e.CurrentItem.AllDay; 
            dr[4] = e.CurrentItem.Content; 
            dr[5] = e.CurrentItem.LabelValue; 
            dr[6] = e.CurrentItem.MarkerValue; 
            dr[7] = e.CurrentItem.Reminder; 
            dr[8] = e.CurrentItem.ReminderValue; 
            dr[9] = e.CurrentItem.LocationValue; 
            dr[10] = e.CurrentItem.UniqueID; 
        } 
    } 
} 
 
Add 
//In SimpleScheduleDataProvider class 
public override void AddItem(IScheduleAppointment item) 
{ 
    DataRow dr = dt.NewRow(); 
    dr[0] = item.StartTime; 
    dr[1] = item.EndTime; 
    dr[2] = item.Subject; 
    dr[3] = item.AllDay; 
    dr[4] = item.Content; 
    dr[5] = item.LabelValue; 
    dr[6] = item.MarkerValue; 
    dr[7] = item.Reminder; 
    dr[8] = item.ReminderValue; 
    dr[9] = item.LocationValue; 
    dr[10] = item.UniqueID; 
    dt.Rows.Add(dr); 
                this.MasterList.Add(item); 
} 
 
Delete 
//In SimpleScheduleDataProvider class 
public override void RemoveItem(IScheduleAppointment item) 
{ 
    var row = dt.Rows.Cast<DataRow>().First(x => x[10].Equals(item.UniqueID.ToString())); 
    int index = dt.Rows.IndexOf(row as DataRow); 
    if (index>-1) 
    { 
        dt.Rows.RemoveAt(index); 
    } 
                this.MasterList.Remove(item); 
} 
 
  
Please get back to us if you need any further assistance on this.  
  
Regards, 
Jagadeesan

Loader.
Live Chat Icon For mobile
Up arrow icon