New to Telerik UI for WinFormsStart a free 30-day trial

How to Show Columns in Resource Headers

Updated over 6 months ago

Environment

Product VersionProductAuthor
2020.3.1020RadScheduler for WinFormsDesislava Yordanova

Description

By default, when RadScheduler is grouped by resources, the resource headers show only the resource name. A common requirement is to show columns and display more information, e.g. Id, Location, etc.

resource-columns-in-radscheduler 001

Solution

The above design can be achieved in RadScheduler using Timeline view with a custom SchedulerResourceHeaderCellElement composed of several LightVisualElements hosted in a horizontal StackLayoutPanel. The custom cell can replace the default one with the help of Scheduler Element Provider .

The following code snippet demonstrates a sample approach:

C#

public static List<Person> employees = new List<Person>();

public RadForm1()
{
    InitializeComponent(); 

    Color[] colors = new Color[]
    {
        Color.LightBlue, Color.LightGreen, Color.LightYellow,
        Color.Red, Color.Orange, Color.Pink, Color.Purple, Color.Peru, Color.PowderBlue
    };
    for (int i = 0; i < 9; i++)
    { 
        employees.Add(new Person(i, i + ". Employee's Name", i + ". Location")); 

        Resource resource = new Resource();
        resource.Id = new EventId(i);
        resource.Name = i + ". Employee's Name";
        resource.Color = colors[i]; 
        this.radScheduler1.Resources.Add(resource);
    }
    
    this.radScheduler1.GroupType = GroupType.Resource;

    this.radScheduler1.ElementProvider = new MyElementProvider(this.radScheduler1);

    this.radScheduler1.ActiveViewType = SchedulerViewType.Timeline;
    this.radScheduler1.ActiveView.ResourcesPerView = 5;

    TimelineGroupingByResourcesElement timelineElement = this.radScheduler1.SchedulerElement.ViewElement as TimelineGroupingByResourcesElement;
    timelineElement.ResourceHeaderWidth = 120; 

    SchedulerCellElement headerCornerCell = timelineElement.FindDescendant<SchedulerCellElement>();
    headerCornerCell.DrawText = true;
    headerCornerCell.Text = "Resources";
}

public static Person GetEmployeeById(int id)
{
    Person m = null;
    foreach (Person item in employees)
    {
        if (item.Id == id)
        {
            m = item;
            break;
        }
    }
    return m;
}

public class MySchedulerResourceHeaderCellElement : SchedulerResourceHeaderCellElement
{ 
    public MySchedulerResourceHeaderCellElement(RadScheduler scheduler, SchedulerView view) : base(scheduler, view)
    {
    }
    
    StackLayoutPanel container = new StackLayoutPanel();
    LightVisualElement idElement = new LightVisualElement();
    LightVisualElement nameElement = new LightVisualElement();
    LightVisualElement locationElement = new LightVisualElement();

    protected override void CreateChildElements()
    {
        base.CreateChildElements();
      
        container.AngleTransform = 90;
        container.StretchHorizontally = true;

        container.Children.Add(idElement);
        idElement.DrawBorder = true;
        idElement.BorderColor = Color.Black;
        idElement.BorderGradientStyle = GradientStyles.Solid; 
        idElement.MinSize = new Size(30, 0);
        idElement.MaxSize = new Size(30, 0);
        idElement.TextWrap = true;

        container.Children.Add(nameElement);
        nameElement.DrawBorder = true;
        nameElement.BorderColor = Color.Black;
        nameElement.BorderGradientStyle = GradientStyles.Solid;
        nameElement.BorderBoxStyle = Telerik.WinControls.BorderBoxStyle.FourBorders;
        nameElement.BorderLeftWidth = 0;
        nameElement.BorderRightWidth = 0;
        nameElement.BorderTopWidth = 1;
        nameElement.BorderTopColor = Color.Black;
        nameElement.BorderBottomWidth = 1;
        nameElement.BorderBottomColor = Color.Black;
        nameElement.MinSize = new Size(50, 0);
        nameElement.MaxSize = new Size(50, 0);
        nameElement.TextWrap = true;

        container.Children.Add(locationElement);
        locationElement.TextWrap = true; 
        locationElement.MinSize = new Size(39, 0);
        locationElement.MaxSize = new Size(39, 0);
        locationElement.DrawBorder = true;
        locationElement.BorderColor = Color.Black;
        locationElement.BorderGradientStyle = GradientStyles.Solid;
        this.Children.Add(container);
        this.DrawText = false;
    }

    public override string Text
    {
        get
        {
            Person m = GetEmployeeById((int)this.ResourceId.KeyValue);
            if (m != null)
            {
                idElement.Text = m.Id.ToString();
                nameElement.Text = m.Name;
                locationElement.Text = m.Location;
            }
            return base.Text;
        }
        set
        {
            base.Text = value;
        }
    }
}

public class MyElementProvider : SchedulerElementProvider
{
    public MyElementProvider(RadScheduler scheduler) : base(scheduler)
    {
    }

    protected override T CreateElement<T>(SchedulerView view, object context)
    {
        if (typeof(T) == typeof(SchedulerResourceHeaderCellElement))
        {
            return new MySchedulerResourceHeaderCellElement(this.Scheduler, view)as T;
        }
        return base.CreateElement<T>(view, context);
    }
}

public class Person
{
    public int Id { get; set; }

    public string Name { get; set; }

    public string Location { get; set; }

    public Person(int id, string name, string location)
    {
        this.Id = id;
        this.Name = name;
        this.Location = location;
    }
}

See Also