Handlers and Grouping disappears after database update

3 posts, 1 answers
  1. Martin Gartmann
    Martin Gartmann avatar
    51 posts
    Member since:
    Apr 2004

    Posted 12 Apr Link to this post

    Dear readers,

    i have a scheduler grouped by resources. I use a dropdown (selectionmode=Multisimple) to switch the resources on/off to show them side by side in the scheduler and AddHandler to each item in the dropdown.

    In Form_Load() i use the following code

          
    ' Populates the dropdown
    ddlResourcen.DataSource = SchedulerDataDataSet.Resources
    ddlResourcen.ValueMember = "ID"
    ddlResourcen.DisplayMember = "Name"
     
     
    For Each item As RadListDataItem In Me.ddlResourcen.Items
        AddHandler item.RadPropertyChanged, AddressOf SelectedChanged
    Next

     

    Here is the code for SelectedChanged()

    Private Sub SelectedChanged()
           AddResources()
       End Sub

    It simply switch the resources on/off.

    I can move appointment between resources, move and resize them and all is fine, until i try to save it to the database.

    I use just the code suggested in the demos with just one change to show my resources in different colors at the last line

    When i call the Sub UpdateDB the stored appointments/resources data is correct, but my handlers for to the drowdown items are not longer

    working and just one resource is visible because my multi preselection is gone.

        Private Sub UpdateDB()
            AppointmentsResourcesTableAdapter.Adapter.AcceptChangesDuringUpdate = False
            Dim deletedRelationRecords As schedulerDataDataSet.AppointmentsResourcesDataTable = TryCast(Me.SchedulerDataDataSet.AppointmentsResources.GetChanges(DataRowState.Deleted), schedulerDataDataSet.AppointmentsResourcesDataTable)
            Dim newRelationRecords As schedulerDataDataSet.AppointmentsResourcesDataTable = TryCast(Me.SchedulerDataDataSet.AppointmentsResources.GetChanges(DataRowState.Added), schedulerDataDataSet.AppointmentsResourcesDataTable)
            Dim modifiedRelationRecords As schedulerDataDataSet.AppointmentsResourcesDataTable = TryCast(Me.SchedulerDataDataSet.AppointmentsResources.GetChanges(DataRowState.Modified), schedulerDataDataSet.AppointmentsResourcesDataTable)
            Dim newAppointmentRecords As schedulerDataDataSet.AppointmentsDataTable = TryCast(Me.SchedulerDataDataSet.Appointments.GetChanges(DataRowState.Added), schedulerDataDataSet.AppointmentsDataTable)
            Dim deletedAppointmentRecords As schedulerDataDataSet.AppointmentsDataTable = TryCast(Me.SchedulerDataDataSet.Appointments.GetChanges(DataRowState.Deleted), schedulerDataDataSet.AppointmentsDataTable)
            Dim modifiedAppointmentRecords As schedulerDataDataSet.AppointmentsDataTable = TryCast(Me.SchedulerDataDataSet.Appointments.GetChanges(DataRowState.Modified), schedulerDataDataSet.AppointmentsDataTable)
            Try
                If newAppointmentRecords IsNot Nothing Then
                    Dim newAppointmentIds As New Dictionary(Of Integer, Integer)()
                    Dim oldAppointmentIds As New Dictionary(Of Object, Integer)()
                    For i As Integer = 0 To newAppointmentRecords.Count - 1
                        oldAppointmentIds.Add(newAppointmentRecords(i), newAppointmentRecords(i).ID)
                    Next
                    AppointmentsTableAdapter.Update(newAppointmentRecords)
                    For i As Integer = 0 To newAppointmentRecords.Count - 1
                        newAppointmentIds.Add(oldAppointmentIds(newAppointmentRecords(i)), newAppointmentRecords(i).ID)
                    Next
                    If newRelationRecords IsNot Nothing Then
                        For i As Integer = 0 To newRelationRecords.Count - 1
                            newRelationRecords(i).AppointmentID = newAppointmentIds(newRelationRecords(i).AppointmentID)
                        Next
                    End If
                End If
                If deletedRelationRecords IsNot Nothing Then
                    AppointmentsResourcesTableAdapter.Update(deletedRelationRecords)
                End If
                If deletedAppointmentRecords IsNot Nothing Then
                    AppointmentsTableAdapter.Update(deletedAppointmentRecords)
                End If
                If modifiedAppointmentRecords IsNot Nothing Then
                    AppointmentsTableAdapter.Update(modifiedAppointmentRecords)
                End If
                If newRelationRecords IsNot Nothing Then
                    AppointmentsResourcesTableAdapter.Update(newRelationRecords)
                End If
                If modifiedRelationRecords IsNot Nothing Then
                    AppointmentsResourcesTableAdapter.Update(modifiedRelationRecords)
                End If
                Me.SchedulerDataDataSet.AcceptChanges()
            Catch ex As Exception
                MessageBox.Show(String.Format("An error occurred during the update process:" & vbLf & "{0}", ex.Message))
            Finally
                If deletedRelationRecords IsNot Nothing Then
                    deletedRelationRecords.Dispose()
                End If
                If newRelationRecords IsNot Nothing Then
                    newRelationRecords.Dispose()
                End If
                If modifiedRelationRecords IsNot Nothing Then
                    modifiedRelationRecords.Dispose()
                End If
            End Try
     
            AddResources()
        End Sub
     
        Private Sub AddResources()
     
            Me.rscMain.Resources.Clear()
            Dim i As Integer = 0
     
            For Each Row As DataRow In SchedulerDataDataSet.Tables("Resources").Rows
                Dim resource As New Resource()
                resource.Id = New EventId(i + 1)
                resource.Name = Row("Name").ToString
                resource.Color = Color.FromName(Row("SystemColor"))
     
                If ddlResourcen.Items(i).Selected = True Then
                    Me.rscMain.Resources.Add(resource)
                End If
     
                i = i + 1
            Next
     
            Me.rscMain.GroupType = GroupType.Resource
            Me.rscMain.ActiveView.ResourcesPerView = 4
        End Sub

     

    I belive this takes places because the SchedulerDataDataSet was changed, because when i reuse the code

    ' Populates the dropdown
    ddlResourcen.DataSource = SchedulerDataDataSet.Resources
    ddlResourcen.ValueMember = "ID"
    ddlResourcen.DisplayMember = "Name"
    For Each item As RadListDataItem In Me.ddlResourcen.Items
        AddHandler item.RadPropertyChanged, AddressOf SelectedChanged
    Next

    i can again use it, but my preselection is gone, of course.

    Any suggestions how to stop this behaviour.

    Kind regards

    Martin Gartmann

  2. Answer
    Dess
    Admin
    Dess avatar
    1609 posts

    Posted 13 Apr Link to this post

    Hello Martin,

    Thank you for writing.

    According to the provided code snippet, you set the RadDropDownList.DataSource property to the Resources collection and subscribe to the RadPropertyChanged event for each item in the drop down. However, when saving to the database, you clear the resources and add new ones. Thus, new items to RadDropDownList are added. Hence, you should subscribe to the RadPropertyChanged event for each of the new items.

    I hope this information helps. Should you have further questions I would be glad to help.

     Regards,
    Dess
    Telerik
    Do you need help with upgrading your AJAX, WPF or WinForms project? Check the Telerik API Analyzer and share your thoughts.
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Martin Gartmann
    Martin Gartmann avatar
    51 posts
    Member since:
    Apr 2004

    Posted 13 Apr in reply to Dess Link to this post

    Dear Dess,

    i solved the case by population the dropdown via a datatable build from another dataset and not the ResourcesCollection. Changing a Resource or Adding a new entry in the Resources Table in the database is a rare case in my app and if this happens i will reload them.

          Me.ResourcesTA.Fill(Me.DsResourcen.Resources)
       
    ' Baut die Resourcenauswahl auf
         ddlResourcen.DataSource = ResourcesTA.GetData
         ddlResourcen.ValueMember = "ID"
         ddlResourcen.DisplayMember = "Name"

    Kind regards

    Martin

Back to Top