Completed
Last Updated: 18 May 2016 09:29 by ADMIN
ADMIN
Dess | Tech Support Engineer, Principal
Created on: 06 May 2016 09:30
Category: GanttView
Type: Bug Report
0
FIX. RadGanttView - NullReferenceException when setting the DataSource to null
To reproduce: use the following code snippet and try to drag an item or click over a task item:

private ViewModel viewModel;

public Form1()
{
    InitializeComponent();
    viewModel = new ViewModel();
    viewModel.Init();
    viewModel.DateChanged += ViewModelOnDateChanged;

    this.radGanttView1.Ratio = 0.3F;
    this.radGanttView1.SplitterWidth = 7;
    this.radGanttView1.TitleMember = "Name";
    this.radGanttView1.ChildMember = "Id";
    this.radGanttView1.EndMember = "End";
    this.radGanttView1.ParentMember = "ParentId";
    this.radGanttView1.ProgressMember = "Progress";
    this.radGanttView1.StartMember = "Start";
    this.radGanttView1.TaskDataMember = "TimingRows";
    this.radGanttView1.LinkDataMember = "LinkRows";
    this.radGanttView1.LinkStartMember = "StartId";
    this.radGanttView1.LinkEndMember = "EndId";
    this.radGanttView1.LinkTypeMember = "LinkType";

    this.radGanttView1.DataSource = viewModel;

    this.radGanttView1.Click += radGanttView1_Click;
}

private void radGanttView1_Click(object sender, EventArgs e)
{ 
    this.radGanttView1.DataSource = null;
    this.radGanttView1.DataSource = viewModel;
}

private void radGanttView1_MouseEnter(object sender, EventArgs e)
{
    this.radGanttView1.DataSource = null;
    this.radGanttView1.DataSource = viewModel;
}

private void ViewModelOnDateChanged(object sender, EventArgs eventArgs)
{
    BeginInvoke(new Action(() =>
    {
        this.radGanttView1.DataSource = null;
        this.radGanttView1.DataSource = viewModel;
    }));
}

public class ViewModel
{
    public event EventHandler DateChanged;

    public BindingList<TimingRow> TimingRows { get; set; }

    public BindingList<LinkRow> LinkRows { get; set; }

    public ViewModel()
    {
        TimingRows = new BindingList<TimingRow>();
        LinkRows = new BindingList<LinkRow>();
    }

    public void Init()
    {
        AddRow(1, 0, "group", 3, 12);
        AddRow(2, 1, "Test1", 3, 4);
        AddRow(3, 1, "Test2", 7, 8);
        AddLink(2, 3, 1);
    }

    private void AddLink(int p1, int p2, int p3)
    {
        var link = new LinkRow(p1, p2, p3);
        LinkRows.Add(link);
    }

    private void AddRow(int id, int parentId, string name, int addDays, int howLong)
    {
        var row = new TimingRow
        {
            Start = DateTime.Today.AddDays(addDays),
            End = DateTime.Today.AddDays(addDays + howLong),
            Id = id,
            ParentId = parentId,
            Name = name
        };
        row.DateChanged += RowOnDateChanged;
        TimingRows.Add(row);
    }

    private void RowOnDateChanged(object sender, EventArgs eventArgs)
    {
        if (DateChanged != null)
            DateChanged(this, EventArgs.Empty);
    }
}

public class TimingRow
{
    public event EventHandler DateChanged;

    private DateTime start;

    public string Name { get; set; }

    public int Id { get; set; }

    public int ParentId { get; set; }

    public decimal Progress
    {
        get
        {
            return 0;
        }
    }

    public virtual DateTime Start
    {
        get
        {
            return start;
        }
        set
        {
            if (start == value)
                return;
            if (DateChanged != null)
                DateChanged(this, EventArgs.Empty);
            start = value;
        }
    }

    public virtual DateTime End { get; set; }
}

public class LinkRow
{
    public int StartId { get; set; }

    public int EndId { get; set; }

    public int LinkType { get; set; }

    public LinkRow(int startId, int endId, int linkType)
    {
        this.StartId = startId;
        this.EndId = endId;
        this.LinkType = linkType;
    }
}
---------------------------------------------------------------------------------
WORKAROUND:

private void radGanttView1_CreateDataItem(object sender, Telerik.WinControls.UI.CreateGanttDataItemEventArgs e)
{
    e.Item = new CustomGanttViewDataItem();
}

public class CustomGanttViewDataItem : GanttViewDataItem
{
    protected override void OnNotifyPropertyChanged(PropertyChangedEventArgs e)
    {
        if (this.GanttViewElement != null)
        {
            base.OnNotifyPropertyChanged(e);
        }
    }
}

public class CustomGanttView : RadGanttView
{
    public override string ThemeClassName
    {
        get
        {
            return typeof(RadGanttView).FullName;
        }
    }

    protected override RadGanttViewElement CreateGanttViewElement()
    {
        return new CustomRadGanttViewElement();
    }
}

public class CustomRadGanttViewElement : RadGanttViewElement
{
    protected override Type ThemeEffectiveType
    {
        get
        {
            return typeof(RadGanttViewElement);
        }
    }

    protected override GanttViewGraphicalViewElement CreateGraphicalViewElement(RadGanttViewElement ganttView)
    {
        return new CustomGanttViewGraphicalViewElement(ganttView);
    }
}

public class CustomGanttViewGraphicalViewElement : GanttViewGraphicalViewElement
{
    protected override Type ThemeEffectiveType
    {
        get
        {
            return typeof(GanttViewGraphicalViewElement);
        }
    }

    public CustomGanttViewGraphicalViewElement(RadGanttViewElement ganttView) : base(ganttView)
    {
    }

    protected override IVirtualizedElementProvider<GanttViewDataItem> CreateElementProvider()
    {
        return new CustomGanttViewVirtualizedElementProvider(this);
    }
}

public class CustomGanttViewVirtualizedElementProvider : GanttViewVirtualizedElementProvider
{
    GanttViewBaseViewElement owner;

    public CustomGanttViewVirtualizedElementProvider(GanttViewBaseViewElement owner) : base(owner)
    {
        this.owner = owner;
    }

    public override IVirtualizedElement<GanttViewDataItem> CreateElement(GanttViewDataItem data, object context)
    {
        GanttViewBaseItemElement element = this.OnItemElementCreating(data);

        if (element != null)
        {
            return element;
        }

        if (owner is GanttViewTextViewElement)
        {
            return new GanttViewTextItemElement(owner as GanttViewTextViewElement);
        }
        else
        {
            if (data.Items.Count > 0)
            {
                return new GanttViewSummaryItemElement(this.owner as GanttViewGraphicalViewElement);
            }
            else if (data.Start == data.End)
            {
                return new GanttViewMilestoneItemElement(this.owner as GanttViewGraphicalViewElement);
            }

            return new CustomGanttViewTaskItemElement(this.owner as GanttViewGraphicalViewElement);
        }
    }
}

public class CustomGanttViewTaskItemElement : GanttViewTaskItemElement
{
    public CustomGanttViewTaskItemElement(GanttViewGraphicalViewElement owner) : base(owner)
    {
    }

    public override void Synchronize()
    {
        if (this.Data.GanttViewElement != null)
        {
            base.Synchronize();
        }
    }
}
Attached Files:
2 comments
ADMIN
Stefan
Posted on: 14 May 2016 10:13
Thank you for your suggestion, I will make sure it gets to our developers. We you can see from the item status, we are currently working on it, so you can expect an official fix in the upcoming Service Pack.
kerman
Posted on: 14 May 2016 09:52