This is a migrated thread and some comments may be shown as answers.

Having problems with CRUD using entities

4 Answers 118 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Scott Tremain
Top achievements
Rank 1
Scott Tremain asked on 18 Dec 2010, 12:03 AM
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

4 Answers, 1 is accepted

Sort by
0
Accepted
Richard Slade
Top achievements
Rank 2
answered on 18 Dec 2010, 07:14 PM
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
0
Accepted
Richard Slade
Top achievements
Rank 2
answered on 18 Dec 2010, 07:18 PM

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
0
Richard Slade
Top achievements
Rank 2
answered on 20 Dec 2010, 01:53 PM
Hello,

Did this help? If so please remember to mark as answer so others can find the solution too.
Regards,
Richard
0
DoomerDGR8
Top achievements
Rank 2
Iron
Iron
Iron
answered on 25 Jan 2011, 05:52 PM
Definitely helped me!
Tags
GridView
Asked by
Scott Tremain
Top achievements
Rank 1
Answers by
Richard Slade
Top achievements
Rank 2
DoomerDGR8
Top achievements
Rank 2
Iron
Iron
Iron
Share this question
or