Sunday, April 12, 2015

MVVM Part 4

Adding missing rows

In the previous examples we were only modifying an existing row. If the row was missing the example would have failed. Let's see what it would take to detect a missing row and add it in code.

The existing code would have failed on the line below because c has no rows.
        Me.ApplicationName = c(0)

To solve this problem simply replace this line with the following...
   If c.Count > 0 Then
       Me.ApplicationName = c(0)
   Else
       ApplicationName = New Config()
       ApplicationName.Key = "ApplicationName"
       ApplicationName.Value = "MVVM_Walkthrough"
       Model.Configs.Add(ApplicationName)
   End If

All we are doing here is detecting the missing row, creating a new row and assigning it to the ViewModel's ApplicationName property, populating it, and adding back into the Config collection as a new row. When the user clicks [Save] it will be added instead of updated.

Now that we have that little Entity Framework nugget out of the way, let's see how we would bind a combobox.

No comments:

Post a Comment