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

radgridview Insert/update/delete for Inheritance Entity throught RIA service

4 Answers 140 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Tai
Top achievements
Rank 1
Tai asked on 29 Sep 2010, 10:42 PM
Hi 
Can you tell me how to make Insert/Delete row work for the radgridview which has the source as a set of Inheritance entity?
I think my problem right now is that i still can't figure out a way to wrap the Ienumerable into an IList, IEditablecollection , or EntityCollectionView because i check on your EntityCollectionView at http://www.telerik.com/community/forums/silverlight/gridview/mvvm-and-gridview-saving-change-ria-services.aspx
and don't see any construction which will accept an Ienumerable as an argument (only EntityCollection or EntitySet). Your help will be appreciated.

This is my situation: 
I have a DocumentLine table and DocumentLineBudget table in the database. By using Entity Framework, I make the DocumentLineBudget entity will inherit from the DocumentLine entity.

Because of this inheritance, when i try to create a Domain service to retrieve DocumentLineBudget list , the normal pop up window(if choosing Domain Service Class template) will grey out the DocumentLineBudget and won't let me choose it.

So I create the operation in my DocumentLineService.cs file to retrieve the list of DocumentLineBudget through the function GetDocumentLineBudgets()
namespace Test_SilverFinancial.Web.Services
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.ComponentModel.DataAnnotations;
    using System.Data;
    using System.Linq;
    using System.ServiceModel.DomainServices.EntityFramework;
    using System.ServiceModel.DomainServices.Hosting;
    using System.ServiceModel.DomainServices.Server;
    using Test_SilverFinancial.Web.Models;
 
 
    // Implements application logic using the SilverFinancialsEntities context.
    // TODO: Add your application logic to these methods or in additional methods.
    // TODO: Wire up authentication (Windows/ASP.NET Forms) and uncomment the following to disable anonymous access
    // Also consider adding roles to restrict access as appropriate.
    // [RequiresAuthentication]
    [EnableClientAccess()]
    public class DocumentLineService : LinqToEntitiesDomainService<SilverFinancialsEntities>
    {
 
        /////////////////////////Extra Operation Services/////////////////////////////
        /// <summary>
        /// GetDocumentLines by DocumentId
        /// </summary>
        /// <param name="documentLine"></param>
        public IQueryable<DocumentLine> GetDocumentLinesById(int documentId)
        {
            return this.ObjectContext.DocumentLines.Where(d => d.DocumentId == documentId);
        }
 
        public IQueryable<DocumentLineBudget> GetDocumentLineBudgets()
        {
            return this.ObjectContext.DocumentLines.OfType<DocumentLineBudget>().AsQueryable();
        }
 
        ////////////////////////////////////////////////////////////////////////////////
 
        // TODO:
        // Consider constraining the results of your query method.  If you need additional input you can
        // add parameters to this method or create additional query methods with different names.
        // To support paging you will need to add ordering to the 'DocumentLines' query.
        public IQueryable<DocumentLine> GetDocumentLines()
        {
            return this.ObjectContext.DocumentLines;
        }
         
 
        public void InsertDocumentLine(DocumentLine documentLine)
        {
            if ((documentLine.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(documentLine, EntityState.Added);
            }
            else
            {
                this.ObjectContext.DocumentLines.AddObject(documentLine);
            }
        }
 
        public void UpdateDocumentLine(DocumentLine currentDocumentLine)
        {
            this.ObjectContext.DocumentLines.AttachAsModified(currentDocumentLine, this.ChangeSet.GetOriginal(currentDocumentLine));
        }
 
        public void DeleteDocumentLine(DocumentLine documentLine)
        {
            if ((documentLine.EntityState == EntityState.Detached))
            {
                this.ObjectContext.DocumentLines.Attach(documentLine);
            }
            this.ObjectContext.DocumentLines.DeleteObject(documentLine);
        }
    }
}

The problem now is that if i use DomainDataSource control which i normally use, then i won't see the GetDocumentLineBudgetsQuery() option on the DataSource toolbar for the DocumentLineContext in order to let me drag and drop the Gridview into the designer.

so my only choice now is not using the DomainDataSource. So i use the code behind like this
namespace Test_SilverFinancial.Windows
{
    public partial class DocumentLineBudgetGridView : RadWindow
 
    {
        private DocumentLineContext dl_ctx;
        public DocumentLineBudgetGridView()
        {
            InitializeComponent();
             
            dl_ctx = new DocumentLineContext();
            EntityQuery<DocumentLineBudget> query = dl_ctx.GetDocumentLineBudgetsQuery();
            LoadOperation<DocumentLineBudget> lop = dl_ctx.Load(query);
             
            documentLineBudgetGridView.ItemsSource = new EntitySetCollectionView<DocumentLine>(dl_ctx.DocumentLines);
        }
 
        private void documentLineBudgetGridView_RowEditEnded(object sender, GridViewRowEditEndedEventArgs e)
        {
            this.dl_ctx.SubmitChanges();
        }
 
        private void documentLineBudgetGridView_Deleted(object sender, GridViewDeletedEventArgs e)
        {
            this.dl_ctx.SubmitChanges();
        }
    }
     
        
}


Right now, i can load the data of the DocumentLineBudget including DocumentLine properties through inheritance properly. However, I can't insert new data back into the database. The Grid won't let me do that

I realize that in my  code i use DocumentLine instead of DocumentLineBudget
documentLineBudgetGridView.ItemsSource = new EntitySetCollectionView<DocumentLine>(dl_ctx.DocumentLines);
So the grid only let me delete but not insert because the property of the DocumentLineBudget won't update.

but if i use, then it won't work too because the EntitySetCollectionView only accept EntitySet argument not an Ienumerable
documentLineBudgetGridView.ItemsSource = new EntitySetCollectionView<DocumentLineBudget>(lop.Entities);


I try to use PageCollectionView to wrap around "lop.Entities" which will return an Ienumerable DocumentLineBudget but also doesn't work

Can you help me how to make the function Insert/ Delete work properly in my case?
what kind of collections does the Radgridview support built-in Insert/Delete? . For my case right now is that the lop.Entities will return an Ienumerable DocumentLineBudgets . If i feed it to the itemssource of the gridview, then i won't get the feature Insert/Delete.



thank you













4 Answers, 1 is accepted

Sort by
0
Tai
Top achievements
Rank 1
answered on 30 Sep 2010, 06:30 PM
some help will be appreciated.
0
Nedyalko Nikolov
Telerik team
answered on 04 Oct 2010, 08:09 AM
Hi Tai,

Generally RadGridView supports automatic add (insert) of a new item for collections that implement IList interface. IList.Add() method is used when new item should be added to RadGridView's item collection.

I would suggest you to handle RadGridView.AddingNewDataItem method and provide necessary object to e.NewObject property (this should do the job in any case).

void radGridView_AddingNewDataItem(object sender, GridViewAddingNewEventArgs e)
        {
            e.NewObject = new BO();
        }

Let me know if this doesn't help.

All the best,
Nedyalko Nikolov
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Tai
Top achievements
Rank 1
answered on 04 Oct 2010, 06:03 PM
Hi Nedyalko,

my lop.Entities will return an Ienumerable DocumentLineBudget  (not EntitySet or EntityCollection, or not even Ilist).
I checked your sample project from other thread and know that we can use customized EntityCollectionView to wrap around an EntitySet or EntityCollection to make the Insert/Delete work.
However, in my case it is an Ienumerable collection (and i don't think there are other way to retrieve the DocumentLineBudget in my case because of the Inheritance issue from Entity Framework)

So i want to know a way to make the Insert/Delete work for my Ienumerable
of course, converting the Ienumerable to Ilist by looping through each data of the collection will be a pain because i might have a lot of data for the DocumentLineBudget.

0
Nedyalko Nikolov
Telerik team
answered on 07 Oct 2010, 06:16 AM
Hi Tai,

Since IEnumerable interface provides only very basic functionality, we cannot perform Insert/Delete operations out of the box. However RadGridView exposes a bunch of events which should help you to meet you goals.
Some of these events are (AddingNewDataItem (for insert), Deleting and Deleted (for Delete), RowEditEnded (for update)). In other words you have to write down your custom IList logic based on IEnumerable data source.
I hope this makes sense.

Best wishes,
Nedyalko Nikolov
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Tags
GridView
Asked by
Tai
Top achievements
Rank 1
Answers by
Tai
Top achievements
Rank 1
Nedyalko Nikolov
Telerik team
Share this question
or