title: Entity Data Model & DataGridView - Creating new objects
body: <p>I'm pretty new to the EDM, so bear with me.  I have a windows form that has a DataGridView on it that's bound from the EDM I created.  I figured out how to update my changes fine, but it's when the user creates a new row that I'm having a problem.</p>
  
  <p>I tried numerous ways and many google searches, but came up with nothing so far.</p>
  
  <p>Here's how the data is loaded:</p>
  
  <pre><code>    Dim boilerID As Integer = DirectCast(ddlBoiler.SelectedValue, Integer)
      Dim countryID As Integer = DirectCast(ddlCountry.SelectedValue, Integer)
  
      Dim ratings = From r In _Context.Rating _
                   Where r.Boiler.Boiler_ID = boilerID _
                   And r.Country.Country_ID = countryID _
                   Order By r.Sequence _
                   Select r
  
      RatingBindingSource.DataSource = ratings.ToList()
  </code></pre>
  
  <p>Also, all I'm doing to save the information right now is the following:</p>
  
  <pre><code>Private Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
      _Context.SaveChanges()
  End Sub
  </code></pre>
  
  <p>I want the user to be able to use the grid's "new row" feature to add new items to the database instead of having to click a button and open a new dialog.  How would this be accomplished?</p>
  
  <p>I can include some of the methods i tried for adding new items if anyone needs to see.</p>
  
  <p>Thanks!</p>
  
  <p>UPDATE: here's one of the methods I tried for adding new objects and the error I receive:</p>
  
  <pre><code>    For Each dgvr As DataGridViewRow In dgvRatings.Rows
          If dgvr.DataBoundItem IsNot Nothing AndAlso CType(dgvr.DataBoundItem, Rating).Rating_ID &lt;= 0 Then
  
              Dim r As Rating = dgvr.DataBoundItem
              r.Boiler = ddlBoiler.SelectedItem
              r.Country = ddlCountry.SelectedItem
  
              _Context.AddObject("Rating", r)
          End If
      Next
  </code></pre>
  
  <p>Error (occurs when calling _Context.SaveChanges()): Store update, insert, or delete statement affected an unexpected number of rows (0). Entities may have been modified or deleted since entities were loaded. Refresh ObjectStateManager entries.</p>