Having problems with CRUD using entities

5 posts, 2 answers
  1. Scott Tremain
    Scott Tremain avatar
    4 posts
    Member since:
    Jan 2009

    Posted 17 Dec 2010 Link to this post

    Ok i am using Entity Framework to access my data, I have no problem adding a record or deleting a record. But when I want to update a record I cant figure out what events to capture.

    I have tried adding a  RadGridView1_RowsChanged to the code and it works great for the update, but my problem occurs now that when I add a record to the grid RadGridView1_RowsChanged gets called before RadGridView1_UserAddedRow.

    In fact the NotifyCollectionChangedAction.ItemChanged gets called before NotifyCollectionChangedAction.Add gets called
    So as soon as I leave a cell in the row my itemchanged fires before the add and i get problems.

    How can i idenitfy if row is being added instead of updated. I sue wish there was a UserUpdatedRow event

    Any help is appreciated

    Public Class ManageMemberType
        Public context As New MIMSModel.MIMSEntities
        Dim newMemType As MemType
        Dim updMemType As MemType
        Dim dmemtype As MemType
        Dim rtype As String
      
      
        Private Sub ManageCompanyType_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            Dim query = (From m In context.MemTypes Select m).ToList
            RadGridView1.DataSource = query
      
        End Sub
      
          
      
      
      
      
        Private Sub RadGridView1_RowsChanged(ByVal sender As Object, ByVal e As Telerik.WinControls.UI.GridViewCollectionChangedEventArgs) Handles RadGridView1.RowsChanged
      
            If e.Action = Telerik.WinControls.Data.NotifyCollectionChangedAction.ItemChanged Then
                Dim updrow As GridViewDataRowInfo = CType(e.NewItems(0), GridViewDataRowInfo)
      
                updMemType = MemType.CreateMemType(updrow.Cells("Type").Value.ToString)
      
                updMemType.Desc = updrow.Cells("Desc").Value.ToString
      
      
                DataAccess.UpdateEntity(updMemType, "MemTypes")
      
            End If
      
      
                End Sub
      
      
      
      
      
      
        Private Sub RadGridView1_UserAddedRow(ByVal sender As Object, ByVal e As Telerik.WinControls.UI.GridViewRowEventArgs) Handles RadGridView1.UserAddedRow
            newMemType = MemType.CreateMemType(e.Row.Cells.Item("Type").Value.ToString)
            newMemType.Desc = e.Row.Cells.Item("Desc").Value.ToString
            Try
                context.MemTypes.AddObject(newMemType)
                context.SaveChanges()
                MsgBox("record Added")
            Catch ex As UpdateException
                e.Row.Delete()
                MsgBox(String.Format("The object could not be added. Make sure that a Member type of '{0}' does not aleady exist.", newMemType.Type))
            End Try
      
      
        End Sub
      
      
        Private Sub RadGridView1_UserDeletingRow(ByVal sender As Object, ByVal e As Telerik.WinControls.UI.GridViewRowCancelEventArgs) Handles RadGridView1.UserDeletingRow
            rtype = e.Rows(0).Cells(0).Value.ToString()
            Dim rcount = (From r In context.GetCompanyInfoes Where r.CompanyType = rtype Select r).Count
            If rcount > 0 Then
                MsgBox("There are  " + rcount.ToString + " companies assigned to this member type, You cannot delete this type")
            Else
                Try
                    dmemtype = MemType.CreateMemType(rtype)
                    DataAccess.DeleteEntity(dmemtype, "MemTypes")
                Catch ex As Exception
                    MsgBox("There was a problem deleting the Member type of " + rtype + " Please notify IT")
                End Try
      
            End If
        End Sub
      
      
    End Class
  2. Answer
    Richard Slade
    Richard Slade avatar
    3000 posts
    Member since:
    May 2009

    Posted 18 Dec 2010 Link to this post

    Hi Scott,

    you can stil use the RowChanged event, but you have to add a little extra to it. Please can you try this. From a short test it seems to work fine.
    Private Sub RadGridView1_RowsChanged(ByVal sender As System.Object, ByVal e As Telerik.WinControls.UI.GridViewCollectionChangedEventArgs) Handles RadGridView1.RowsChanged
        If e.Action = NotifyCollectionChangedAction.Add Then
            MessageBox.Show("New row")
        Else
            If Me.RadGridView1.CurrentRow IsNot Nothing Then
                If Me.RadGridView1.CurrentRow.Index > -1 AndAlso e.Action = NotifyCollectionChangedAction.ItemChanged Then
                    MessageBox.Show("Row Changed")
                End If
            End If
        End If
    End Sub

    Hope that helps
    Richard
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Answer
    Richard Slade
    Richard Slade avatar
    3000 posts
    Member since:
    May 2009

    Posted 18 Dec 2010 Link to this post

    Forgot to add in row deleted too, so you can do all three from the one event.

    Private Sub RadGridView1_RowsChanged(ByVal sender As System.Object, ByVal e As Telerik.WinControls.UI.GridViewCollectionChangedEventArgs) Handles RadGridView1.RowsChanged
        If e.Action = NotifyCollectionChangedAction.Add Then
            MessageBox.Show("New row")
        Else
            If Me.RadGridView1.CurrentRow IsNot Nothing Then
                If Me.RadGridView1.CurrentRow.Index > -1 AndAlso e.Action = NotifyCollectionChangedAction.ItemChanged Then
                    MessageBox.Show("Row Changed")
                End If
                If e.Action = NotifyCollectionChangedAction.Remove Then
                    MessageBox.Show("Row Deleted")
                End If
            End If
        End If
    End Sub
    Richard
  5. Richard Slade
    Richard Slade avatar
    3000 posts
    Member since:
    May 2009

    Posted 20 Dec 2010 Link to this post

    Hello,

    Did this help? If so please remember to mark as answer so others can find the solution too.
    Regards,
    Richard
  6. DoomerDGR8
    DoomerDGR8 avatar
    133 posts
    Member since:
    Aug 2010

    Posted 25 Jan 2011 Link to this post

    Definitely helped me!
Back to Top
UI for WinForms is Visual Studio 2017 Ready