Last Updated: 10 Nov 2014 12:26 by ADMIN
Dess | Tech Support Engineer, Principal
Created on: 27 Oct 2014 15:47
Category: GanttView
Type: Bug Report
FIX. RadGanttView - NullReferenceException when rebinding and restoring the SelectedItem
To reproduce: use the following code snippet and perform the described steps below:

Sub New()

End Sub

Private Sub PopulateData()

    Dim tasks As New DataTable("Tasks")
    tasks.Columns.Add("Id", GetType(Integer))
    tasks.Columns.Add("ParentId", GetType(Integer))
    tasks.Columns.Add("Title", GetType(String))
    tasks.Columns.Add("Start", GetType(DateTime))
    tasks.Columns.Add("End", GetType(DateTime))
    tasks.Columns.Add("Progress", GetType(Decimal))

    Dim links As New DataTable("Links")
    links.Columns.Add("StartId", GetType(Integer))
    links.Columns.Add("EndId", GetType(Integer))
    links.Columns.Add("LinkType", GetType(Integer))

    Dim data As New DataSet()

    tasks.Rows.Add(1, 0, "Summary task title", New DateTime(2010, 10, 10), New DateTime(2010, 10, 15), 30D)
    tasks.Rows.Add(2, 1, "First child task title", New DateTime(2010, 10, 10), New DateTime(2010, 10, 12), 10)
    tasks.Rows.Add(3, 1, "Second child task title", New DateTime(2010, 10, 12), New DateTime(2010, 10, 15), 20D)
    tasks.Rows.Add(4, 1, "Milestone", New DateTime(2010, 10, 15), New DateTime(2010, 10, 15), 0D)

    links.Rows.Add(2, 3, 1)
    links.Rows.Add(3, 4, 1)

    Me.RadGanttView1.GanttViewElement.TaskDataMember = "Tasks"
    Me.RadGanttView1.GanttViewElement.ChildMember = "Id"
    Me.RadGanttView1.GanttViewElement.ParentMember = "ParentId"
    Me.RadGanttView1.GanttViewElement.TitleMember = "Title"
    Me.RadGanttView1.GanttViewElement.StartMember = "Start"
    Me.RadGanttView1.GanttViewElement.EndMember = "End"
    Me.RadGanttView1.GanttViewElement.ProgressMember = "Progress"
    Me.RadGanttView1.GanttViewElement.LinkDataMember = "Links"
    Me.RadGanttView1.GanttViewElement.LinkStartMember = "StartId"
    Me.RadGanttView1.GanttViewElement.LinkEndMember = "EndId"
    Me.RadGanttView1.GanttViewElement.LinkTypeMember = "LinkType"

    Me.RadGanttView1.GanttViewElement.DataSource = data

    Me.RadGanttView1.GanttViewElement.GraphicalViewElement.TimelineStart = New DateTime(2010, 10, 9)
    Me.RadGanttView1.GanttViewElement.GraphicalViewElement.TimelineEnd = New DateTime(2010, 12, 10)
End Sub

Private Sub RadButton1_Click(sender As Object, e As EventArgs) Handles RadButton1.Click
    Dim selected = Me.RadGanttView1.SelectedItem.Title

    Me.RadGanttView1.DataSource = Nothing

    Dim result = FindDataItem(selected, Me.RadGanttView1.Items)

    If result IsNot Nothing Then
        Me.RadGanttView1.SelectedItem = result
    End If
End Sub

Dim found = Nothing
Private Function FindDataItem(selected As String, items As GanttViewDataItemCollection) As GanttViewDataItem

    For Each item As GanttViewDataItem In items
        If item.Title = selected Then
            Return item
        End If

        Dim itm As GanttViewDataItem = FindDataItem(selected, item.Items)

        If itm IsNot Nothing Then
            Return itm
        End If
    Return Nothing
End Function

1.Select a random task item
2.Click the button to rebind and restore the selection
As a result you will encounter a NullReferenceException.

Workaround: set the SelectedItem property to null/Nothing before rebinding