This is a migrated thread and some comments may be shown as answers.

Drag & Drop using resource view

11 Answers 193 Views
Scheduler and Reminder
This is a migrated thread and some comments may be shown as answers.
Gerben
Top achievements
Rank 1
Gerben asked on 07 Feb 2011, 02:05 PM
Hi all,

Is it possible to get drag & drop working using multiple resources because I can't find anything about this. I've got the drag and drop working just fine for the standard view (just one resource), but when I set the GroupType of the radscheduler to GroupType.Resource, it won't work.

An other questing:
How can I get multple resources on the same screen. I can only get 2 resources at my screen, and I need to use a scrollbar to get to the next resource, so is there a property I need to set somewhere?

11 Answers, 1 is accepted

Sort by
0
Richard Slade
Top achievements
Rank 2
answered on 07 Feb 2011, 02:18 PM
hello,

At this stage, I'm not sure about the drag and drop. I will have to try this. Can you provide an exmaple or what you are trying to do?
For the multiple resources part, please take a look at the documentation at this link.
Hope that helps
Richard
0
Gerben
Top achievements
Rank 1
answered on 07 Feb 2011, 03:09 PM
The solution for the showing multiple resources only works as long as you don't change the view.
for example I start at the day view, showing only 1 day for each resource. When i switch to week view, and then switch back, I'm seeing 3 days for every resource, and only 2 resources in the total view.
I used this code to set the dayview to 1 day and the amount of resources to 4:
this.radScheduler1.GetDayView().DayCount = 1;
this.radScheduler1.GetDayView().ResourcesPerView = 4;

I'm using the 2010.3.10.1215 build.

I've got the following Code: (sorry for the amount of code, but I can't add an archive to this topic)
Form1
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Telerik.WinControls;
using Telerik.WinControls.UI;
 
namespace RadSchedulerDummy
{
    public enum AppointmentFields
    {
        Row,
        Summary,
        Location,
        Description,
        Start,
        End,
        Background,
        Status
    }
 
    public class DragObject
    {
        private AppointmentFields status;
 
        private Dictionary<AppointmentFields, object> values = new Dictionary<AppointmentFields, object>();
 
        public AppointmentFields Status
        {
            get
            {
                return this.status;
            }
            set
            {
                this.status = value;
            }
        }
 
        public Dictionary<AppointmentFields, object> Values
        {
            get
            {
                return this.values;
            }
        }
    }
 
    public partial class RadForm1 : Telerik.WinControls.UI.RadForm
    {
        private List<Meeting> meetings = new List<Meeting>();
 
        public RadForm1()
        {
            InitializeComponent();
            Resource r1 = new Resource();
            r1.Name = "Office 1";
            r1.Color = Color.Blue;
 
            Resource r2 = new Resource();
            r2.Name = "Office 2";
            r2.Color = Color.Red;
 
            Resource r3 = new Resource();
            r3.Name = "Office 3";
            r3.Color = Color.Gray;
 
            Resource r4 = new Resource();
            r4.Name = "Office 4";
            r4.Color = Color.Yellow;
 
            this.radScheduler1.Resources.Add(r1);
            this.radScheduler1.Resources.Add(r2);
            this.radScheduler1.Resources.Add(r3);
            this.radScheduler1.Resources.Add(r4);
            this.radScheduler1.GroupType = GroupType.Resource;
            this.radScheduler1.AllowDrop = true;
            this.radScheduler1.DragEnter += new DragEventHandler(radScheduler1_DragEnter);
            this.radScheduler1.DragDrop += new DragEventHandler(radScheduler1_DragDrop);
 
            this.radScheduler1.GetDayView().DayCount = 1;
            this.radScheduler1.GetDayView().ResourcesPerView = 4;
            this.FillGrid();
            this.radGridView1.DataSource = this.meetings;
            this.radGridView1.GridBehavior = new MyGridBehavior();
        }
 
        void radScheduler1_DragDrop(object sender, DragEventArgs e)
        {
            Point point = this.radScheduler1.PointToClient(new Point(e.X, e.Y));
 
            DayViewAppointmentsTable table = (this.radScheduler1.SchedulerElement.ViewElement as SchedulerDayViewElement).DataAreaElement.Table;
 
            SchedulerCellElement schedulerCell = SchedulerUIHelper.GetCellAtPoint(point, table.Children);
 
 
            DragObject dragObject = e.Data.GetData(typeof(DragObject)) as DragObject;
            if (dragObject != null)
            {
                this.radScheduler1.Appointments.BeginUpdate();
                Appointment appointment = CreateAppointment(schedulerCell.Date, dragObject);
                this.radScheduler1.Appointments.Add(appointment);
                this.radScheduler1.Appointments.EndUpdate();
                this.radGridView1.Rows.RemoveAt(this.radGridView1.SelectedRows[0].Index);
            }
        }
 
        void radScheduler1_DragEnter(object sender, DragEventArgs e)
        {
            DragObject dragObject = e.Data.GetData(typeof(DragObject)) as DragObject;
 
            e.Effect = dragObject == null ? DragDropEffects.None : DragDropEffects.Copy;
        }
 
        public Appointment CreateAppointment(DateTime currentDate, DragObject dragObject)
        {
            Appointment appointment = new Appointment();
            DateTime startDate = currentDate;
            DateTime endDate = currentDate.AddHours(1);
            switch (dragObject.Status)
            {
                case AppointmentFields.Summary:
                    appointment.Summary = dragObject.Values[dragObject.Status] as string;
                    break;
                case AppointmentFields.Row:
                    appointment.Summary = dragObject.Values[AppointmentFields.Summary] as string;
                    appointment.Description = dragObject.Values[AppointmentFields.Description] as string;
                    appointment.BackgroundId = (int)dragObject.Values[AppointmentFields.Background];
                    appointment.StatusId = (int)dragObject.Values[AppointmentFields.Status];
                    appointment.Location = dragObject.Values[AppointmentFields.Location] as string;
 
                    startDate = (DateTime)dragObject.Values[AppointmentFields.Start];
                    endDate = (DateTime)dragObject.Values[AppointmentFields.End];
 
                    TimeSpan range = endDate - startDate;
                    endDate = currentDate + range;
                    startDate = currentDate;
 
                    break;
            }
            appointment.Start = startDate;
            appointment.End = endDate;
            return appointment;
        }
 
 
        private void FillGrid()
        {
            for (int i = 1; i < 20; i++)
            {
                this.meetings.Add(new Meeting(i, "Meeting " + i, 1, 3));
            }
        }
 
 
 
 
        private class MyGridBehavior : BaseGridBehavior
        {
            private Point mouseDownPoint = Point.Empty;
 
            public override bool OnMouseDown(MouseEventArgs e)
            {
                if (e.Button == MouseButtons.Left)
                {
                    mouseDownPoint = e.Location;
                }
 
                return base.OnMouseDown(e);
            }
 
            public override bool OnMouseMove(MouseEventArgs e)
            {
                if (e.Button != MouseButtons.Left)
                {
                    this.mouseDownPoint = Point.Empty;
                    return base.OnMouseMove(e);
                }
 
                if (!this.GridViewElement.IsInEditMode && this.ShouldBeginDrag(this.mouseDownPoint, e.Location))
                {
                    RadElement element = this.GridControl.ElementTree.GetElementAtPoint(e.Location);
 
                    GridCellElement cell = element as GridCellElement;
 
                    if (cell != null && cell == this.GridViewElement.CurrentCell)
                    {
                        DragObject dragObject = new DragObject();
 
                        dragObject.Values.Add(AppointmentFields.Summary, "Meeting: " + cell.RowInfo.Cells["ID"].Value);
                        dragObject.Values.Add(AppointmentFields.Start, DateTime.Now);
                        dragObject.Values.Add(AppointmentFields.End, DateTime.Now.AddHours(1));
                        dragObject.Values.Add(AppointmentFields.Description, "Meeting");
                        dragObject.Values.Add(AppointmentFields.Background, 1);
                        dragObject.Values.Add(AppointmentFields.Status, 1);
                        dragObject.Values.Add(AppointmentFields.Location, "Office");
 
                        dragObject.Status = AppointmentFields.Row;
 
                        this.GridViewElement.ElementTree.Control.DoDragDrop(dragObject, DragDropEffects.Copy);
                        return true;
                    }
                }
 
                return base.OnMouseMove(e);
            }
 
 
            private bool ShouldBeginDrag(Point current, Point capture)
            {
                Size dragSize = SystemInformation.DragSize;
                Rectangle dragRect = new Rectangle(capture.X - dragSize.Width / 2,
                                                   capture.Y - dragSize.Height / 2,
                                                   dragSize.Width, dragSize.Height);
                return !dragRect.Contains(current);
            }
        }
    }
}

Designer file
partial class RadForm1
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;
 
        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }
 
        #region Windows Form Designer generated code
 
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            Telerik.WinControls.UI.DateTimeInterval dateTimeInterval1 = new Telerik.WinControls.UI.DateTimeInterval();
            this.radGridView1 = new Telerik.WinControls.UI.RadGridView();
            this.radSchedulerNavigator1 = new Telerik.WinControls.UI.RadSchedulerNavigator();
            this.radScheduler1 = new Telerik.WinControls.UI.RadScheduler();
            ((System.ComponentModel.ISupportInitialize)(this.radGridView1)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.radGridView1.MasterTemplate)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.radSchedulerNavigator1)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.radScheduler1)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this)).BeginInit();
            this.SuspendLayout();
            //
            // radGridView1
            //
            this.radGridView1.Dock = System.Windows.Forms.DockStyle.Bottom;
            this.radGridView1.Location = new System.Drawing.Point(0, 418);
            this.radGridView1.Name = "radGridView1";
            this.radGridView1.Size = new System.Drawing.Size(1242, 150);
            this.radGridView1.TabIndex = 0;
            this.radGridView1.Text = "radGridView1";
            //
            // radSchedulerNavigator1
            //
            this.radSchedulerNavigator1.AssociatedScheduler = this.radScheduler1;
            this.radSchedulerNavigator1.DateFormat = "yyyy/MM/dd";
            this.radSchedulerNavigator1.Dock = System.Windows.Forms.DockStyle.Top;
            this.radSchedulerNavigator1.Location = new System.Drawing.Point(0, 0);
            this.radSchedulerNavigator1.Name = "radSchedulerNavigator1";
            this.radSchedulerNavigator1.NavigationStepType = Telerik.WinControls.UI.NavigationStepTypes.Day;
            //
            //
            //
            this.radSchedulerNavigator1.RootElement.StretchVertically = false;
            this.radSchedulerNavigator1.Size = new System.Drawing.Size(1242, 77);
            this.radSchedulerNavigator1.TabIndex = 1;
            this.radSchedulerNavigator1.Text = "radSchedulerNavigator1";
            //
            // radScheduler1
            //
            dateTimeInterval1.End = new System.DateTime(((long)(0)));
            this.radScheduler1.AccessibleInterval = dateTimeInterval1;
            this.radScheduler1.AppointmentTitleFormat = null;
            this.radScheduler1.Culture = new System.Globalization.CultureInfo("nl-NL");
            this.radScheduler1.DataSource = null;
            this.radScheduler1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.radScheduler1.GroupType = Telerik.WinControls.UI.GroupType.None;
            this.radScheduler1.HeaderFormat = "dd dddd";
            this.radScheduler1.Location = new System.Drawing.Point(0, 77);
            this.radScheduler1.Name = "radScheduler1";
            this.radScheduler1.Padding = new System.Windows.Forms.Padding(0, 0, 0, 1);
            this.radScheduler1.Size = new System.Drawing.Size(1242, 341);
            this.radScheduler1.TabIndex = 2;
            this.radScheduler1.Text = "radScheduler1";
            //
            // RadForm1
            //
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(1242, 568);
            this.Controls.Add(this.radScheduler1);
            this.Controls.Add(this.radSchedulerNavigator1);
            this.Controls.Add(this.radGridView1);
            this.Name = "RadForm1";
            //
            //
            //
            this.RootElement.ApplyShapeToControl = true;
            this.Text = "RadForm1";
            this.ThemeName = "ControlDefault";
            ((System.ComponentModel.ISupportInitialize)(this.radGridView1.MasterTemplate)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.radGridView1)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.radSchedulerNavigator1)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.radScheduler1)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this)).EndInit();
            this.ResumeLayout(false);
            this.PerformLayout();
 
        }
 
        #endregion
 
        private Telerik.WinControls.UI.RadGridView radGridView1;
        private Telerik.WinControls.UI.RadSchedulerNavigator radSchedulerNavigator1;
        private Telerik.WinControls.UI.RadScheduler radScheduler1;
    }

Meeting file
class Meeting
    {
        public int Id { get; set; }
        public String Name { get; set; }
        public int DurationHours { get; set; }
        public int People { get; set; }
 
 
        public Meeting(int id, string name, int duration, int nrPeople)
        {
            this.Id = id;
            this.Name = name;
            this.DurationHours = duration;
            this.People = nrPeople;
        }
 
    }
0
Richard Slade
Top achievements
Rank 2
answered on 07 Feb 2011, 03:38 PM
Hello,

When you change the Active View, you will need to re-assign the ResourcesPerView.
Have a look at this blog article which explains.
hope that helps
Richard
0
Gerben
Top achievements
Rank 1
answered on 08 Feb 2011, 09:00 AM
Thanks for this info. The issue with the view has been resolved. Have you checked out the code for the drag & drop?
0
Richard Slade
Top achievements
Rank 2
answered on 08 Feb 2011, 10:38 AM
Hello,

Yes, I'm having a look at it, but haven't come up with a working solution at present. I'll let you know as soon as I do/if I can.
regards,
Richard
0
Richard Slade
Top achievements
Rank 2
answered on 08 Feb 2011, 11:50 AM
Hello,

When you perform drag and drop for a scheduler grouped by resource, your resources need ids, and you need to get back the resource Id for the cell that you are dropping to .

I have altered the following code. Please can you try this.

Form Load. Now adds in a resource Id for each resource
public Form1()
{
    RadControlSpyForm f = new RadControlSpyForm();
    f.Show();
    InitializeComponent();
    Resource r1 = new Resource();
    r1.Name = "Office 1";
    r1.Id = new EventId(1);
    r1.Color = Color.Blue;
    Resource r2 = new Resource();
    r2.Name = "Office 2";
    r2.Id = new EventId(2);
    r2.Color = Color.Red;
    Resource r3 = new Resource();
    r3.Name = "Office 3";
    r3.Id = new EventId(3);
    r3.Color = Color.Gray;
    Resource r4 = new Resource();
    r4.Name = "Office 4";
    r4.Id = new EventId(4);
    r4.Color = Color.Yellow;
    this.radScheduler1.Resources.Add(r1);
    this.radScheduler1.Resources.Add(r2);
    this.radScheduler1.Resources.Add(r3);
    this.radScheduler1.Resources.Add(r4);
    this.radScheduler1.GroupType = GroupType.Resource;
    this.radScheduler1.AllowDrop = true;
    this.radScheduler1.DragEnter += new DragEventHandler(radScheduler1_DragEnter);
    this.radScheduler1.DragDrop += new DragEventHandler(radScheduler1_DragDrop);
    this.radScheduler1.GetDayView().DayCount = 1;
    this.radScheduler1.GetDayView().ResourcesPerView = 4;
    this.FillGrid();
    this.radGridView1.DataSource = this.meetings;
    this.radGridView1.GridBehavior = new MyGridBehavior();
}

DragDrop event, now just gets the cell element from ElementAtPoint method, and gets the resource id required to create the appointment in the right place (against the right resource) from the DayViewBase
void radScheduler1_DragDrop(object sender, DragEventArgs e)
{
    Point point = this.radScheduler1.PointToClient(new Point(e.X, e.Y));
    SchedulerCellElement schedulerCell = (SchedulerCellElement)this.radScheduler1.ElementTree.GetElementAtPoint(point);
    EventId id = ((Telerik.WinControls.UI.DayViewAppointmentsTable)schedulerCell.Parent).DayViewBase.GetResources()[0].Id;
      
    DragObject dragObject = e.Data.GetData(typeof(DragObject)) as DragObject;
    if (dragObject != null)
    {
        this.radScheduler1.Appointments.BeginUpdate();
        Appointment appointment = CreateAppointment(schedulerCell.Date, dragObject);
        appointment.ResourceId = id;
        this.radScheduler1.Appointments.Add(appointment);
        this.radScheduler1.Appointments.EndUpdate();
        this.radGridView1.Rows.RemoveAt(this.radGridView1.SelectedRows[0].Index);
    }
}

Hope that helps
Richard
0
Richard Slade
Top achievements
Rank 2
answered on 08 Feb 2011, 01:14 PM
Hi again,

I've been testing what I sent you and it seems to work really well. The only thing is that it currently errors if you try and drag an appointment over an existing appointment. What did you want to do in this situation? (Ie - create a second appointment or disallow)
thanks
Richard
0
Gerben
Top achievements
Rank 1
answered on 08 Feb 2011, 01:19 PM
Hi Richard,

Thanks for the sample. As answer to your question. Creating a second appointment would be nice. 
0
Accepted
Richard Slade
Top achievements
Rank 2
answered on 08 Feb 2011, 01:37 PM
Hi Gerben,

This should do the job. May need a little tidying up, but it seems to work fine.

void radScheduler1_DragDrop(object sender, DragEventArgs e)
{
    Point point = this.radScheduler1.PointToClient(new Point(e.X, e.Y));
    SchedulerCellElement schedulerCell;
    AppointmentElement appointmentElement;
    EventId id = new EventId(0);
    DateTime date = new DateTime();
    if (this.radScheduler1.ElementTree.GetElementAtPoint(point) is SchedulerCellElement)
    {
        schedulerCell = (SchedulerCellElement)this.radScheduler1.ElementTree.GetElementAtPoint(point);
        id = ((Telerik.WinControls.UI.DayViewAppointmentsTable)schedulerCell.Parent).DayViewBase.GetResources()[0].Id;
        date = schedulerCell.Date;
    }
    else if (this.radScheduler1.ElementTree.GetElementAtPoint(point) is AppointmentElement)
    {
        appointmentElement = (AppointmentElement)this.radScheduler1.ElementTree.GetElementAtPoint(point);
        id = ((Telerik.WinControls.UI.DayViewAppointmentsTable)appointmentElement.Parent).DayViewBase.GetResources()[0].Id;
        date = appointmentElement.Start;
    }
      
    DragObject dragObject = e.Data.GetData(typeof(DragObject)) as DragObject;
    if (dragObject != null)
    {
        this.radScheduler1.Appointments.BeginUpdate();
        Appointment appointment = CreateAppointment(date, dragObject);
        appointment.ResourceId = id;
        this.radScheduler1.Appointments.Add(appointment);
        this.radScheduler1.Appointments.EndUpdate();
        this.radGridView1.Rows.RemoveAt(this.radGridView1.SelectedRows[0].Index);
    }
}

hope that helps
Richard
0
Gerben
Top achievements
Rank 1
answered on 14 Feb 2011, 10:27 AM
Thanks a lot. This was exactly what I needed.
0
Richard Slade
Top achievements
Rank 2
answered on 14 Feb 2011, 10:51 AM
Glad I could be of help.
All the best
Richard
Tags
Scheduler and Reminder
Asked by
Gerben
Top achievements
Rank 1
Answers by
Richard Slade
Top achievements
Rank 2
Gerben
Top achievements
Rank 1
Share this question
or