locked
Adding data to first (empty) row of LightSwitch Data Grid..... RRS feed

  • Question

  • Would someone please help. From one screen, I am passing parameters to another screen which uses these parameters to query a Milestone Tasks table in the receiving screen and populating a LightSwitch EditableDataGrid.  It works fine as long as the query finds records to populate the grid ("If" first part of program below).  When there is no data to populate the grid, I want to populate the first new, empty row displayed with a little of my own data.  I can't seem to get at that first row.  Somehow the first empty grid row gets built after the procedure below "somewhere".  I tried putting code after the "Else" statement but it doesn't like it.  Is there another "Declareative" area I should be using.  Thanks 

            Private Sub MilestoneTasks1_Changed(e As Collections.Specialized.NotifyCollectionChangedEventArgs)
                ' Copy data from the last row in grid when the user adds a new row and provide default values based on last data.
                If e.Action = Collections.Specialized.NotifyCollectionChangedAction.Add Then
                    If Me.MilestoneTasks1.Count > 1 And e.NewItems.Count = 1 Then
                        Try
                            Dim newTask As MilestoneTask = e.NewItems(0)
                            Dim currentTask As MilestoneTask = Me.MilestoneTasks1(e.NewStartingIndex - 1)

                            newTask.MilestoneTaskLN = currentTask.MilestoneTaskLN
                            newTask.MilestoneTaskLN = newTask.MilestoneTaskLN + 1  ' Incremented line number by 1

                            newTask.ProjectID = currentTask.ProjectID
                            newTask.MilestoneID = currentTask.MilestoneID
                            newTask.MilestoneTaskRespID = currentTask.MilestoneTaskRespID
                            newTask.NPRID = currentTask.NPRID
                            newTask.PPAPLevelID = currentTask.PPAPLevelID
                            newTask.Project = currentTask.Project
                            newTask.Milestone = currentTask.Milestone
                            newTask.NPR_Datum = currentTask.NPR_Datum
                            newTask.Status1 = currentTask.Status1
                        Catch ex As Exception
                            Trace.TraceInformation("Could not copy data into new row " + ex.ToString)
                        End Try
                    End If
                Else
                    ' No data exists for first row, add 1 to MilestoneTaskLN data column (first row, first column of grid)
                    If Me.MilestoneTasks1.Count = 0 Then
                        Dim newTask As MilestoneTask = e.NewItems(1)
                        Dim currentTask As MilestoneTask = Me.MilestoneTasks1(e.NewStartingIndex)

                        newTask.MilestoneTaskLN = currentTask.MilestoneTaskLN
                        newTask.MilestoneTaskLN = newTask.MilestoneTaskLN       ' Set first line number to 1
                    End If
                End If
            End Sub

           


    Edward Piccoli

    Thursday, November 7, 2013 2:08 PM

Answers

  • Hi Edward,

    It looks like there's a logic flaw in your else statement. The e.NewItems will typically only have 1 item in the collection because a user can only add one record at a time, so the new row would be e.NewItems(0). Also the e.NewStartingIndex would give you the NEW row's index. So if there are no items in the collection (which is the else case) then the new and current item will be the same object as you've written it.

    If you skip to 3:30 on this video, I show a similar code pattern for setting a new row of data to the previous row's data. http://msdn.microsoft.com/en-us/vstudio/hh307862

    You also may want to look into the _Created method on your entity instead, which allows you to set default values on entities.

    HTH,

    -Beth


    Senior Program Manager, Visual Studio Community http://www.bethmassi.com http://msdn.com/lightswitch http://dev.office.com

    Friday, November 8, 2013 1:15 AM