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

[Solved] RIA Services + AutoGenerated Columns + Child Entities

5 Answers 128 Views
GridView
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Matthew Kruczek
Top achievements
Rank 1
Matthew Kruczek asked on 11 Aug 2011, 10:48 PM
Ok so here is my problem...

I have a back end DAL layer that uses Entity Framework and RIA Services.  On the Client side I have a GridView object that I have set up so that I can bind any entity I want to it by using generics...(see below)...now my problem comes into play when I have a Child entity in the entity I'm binding to...

So for example If I have a Customer Entity that has a child entity of Address.  When I generate the GridView I get a column that has the entire Address Entity inside it....What I want is to Display one property off of that child entity like (AddressName).  Similar to the way you have a "DisplayMemberPath" inside a combobox.

I need to do this so I code as LITTLE specific code as possible and keep it as Generic as I can.  I have tried catching the "AutoGeneratingColumn" event that seems to give me access to the Datacolumn but I'm unsure how to alter it to display the correct value.

Thoughts?

protected void SubscribedEvent_UpdateFilter<T>(IEnumerable<T> entityCollection)
                    {
 
                        if ((MyRadGridView != null) && (MyRadGridView.ItemsSource == null) && (entityCollection != null))
                        {
                            // Retrieve the Type of the Collection
                            _myType = entityCollection.GetType();
 
                            // Reset Selected Items
                            SelectedItems = new ObservableCollection<dynamic>();
 
 
                            // Bind all of the Events
                            //this.MyRadGridView.RowLoaded
                            this.MyRadGridView.AutoGeneratingColumn += new EventHandler<GridViewAutoGeneratingColumnEventArgs>(MyRadGridView_AutoGeneratingColumn);
                            this.MyRadGridView.SelectionChanged += new System.EventHandler<SelectionChangeEventArgs>(MyRadGridView_SelectionChanged);
                            this.MyRadGridView.Items.PageChanged += new System.EventHandler<System.EventArgs>(MyRadGridViewItems_PageChanged);
                            this.MyRadDataPager.PageIndexChanging += new System.EventHandler<PageIndexChangingEventArgs>(MyRadDataPager_PageIndexChanging);
 
                            // Bind All of the Sources
                            this.MyRadGridView.ItemsSource = entityCollection;
                            this.MyRadDataFilter.Source = MyRadGridView.Items;
                            this.MyRadDataPager.Source = MyRadGridView.Items;
                        }
                    }

5 Answers, 1 is accepted

Sort by
0
Rossen Hristov
Telerik team
answered on 12 Aug 2011, 08:29 AM
Hello Matthew Kruczek,

I think that the proper way to stop a column from being auto-generated would be to use the DisplayAttribute.AutoGenerateField=false Data Annotation. We will respect that data annotation on the client.

Then once you stop the Address column from auto-generating, you can manually add a new column which can have a DataMemberBinding to "Address.Name".

Can you try this and see how it goes.

All the best,
Ross
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get now >>

0
Matthew Kruczek
Top achievements
Rank 1
answered on 12 Aug 2011, 04:04 PM
Yup that worked, thanks.
0
Matthew Kruczek
Top achievements
Rank 1
answered on 12 Aug 2011, 08:37 PM
0
Matthew Kruczek
Top achievements
Rank 1
answered on 12 Aug 2011, 08:41 PM
Ok so now that I have that squared away here is my next issue...

 I have an entity  that is decorated with the [Include] attribute on the server side.  

 internal sealed class EventMetadata
       
{

           
// Metadata classes are not meant to be instantiated.
           
private EventMetadata()
           
{
           
}
           
           
[Include]
           
[Display(AutoGenerateField = false)]
           
public EventGroup EventGroup { get; set; }

I also have a query that includes the "EventGroup" piece in an .Include("EventGroup") statement.

        public IQueryable<Event> GetEvents()
       
{

           
return this.ObjectContext.Events.Include("EventGroup").Include("EventType");
       
}

Here is the problem....when I get to the client side I am taking this entity and loading it into the grid. The problem is that the Event.EventType is null when it get's to my client. However..

If I do this...

var events = _getAllEventsLoadOperation.Entities.Cast<Event>().ToList();
               
foreach (Event item in events)
               
{
                   
if ((item.EventGroup == null) & (item.EventType == null)) { }
               
}
It populates....
Basically what I think this means is that I actually have to touch each and every child entity before it will load.  I don't want to have to do this...is there something I can do that will preload them?  Loading them into the grid doesn't do it, and I don't want to have to write code like this for every lazy loaded entity I'm using.
0
Nedyalko Nikolov
Telerik team
answered on 18 Aug 2011, 02:09 PM
Hello Matthew Kruczek,

Sorry for the late reply.
I'm attaching my test project, based on a Northwind database hosted on a SQLEXPRESS SQL server instance (you can change connection string inside web.config file according to your machine setup). This project gets all entity types correctly even with include clause.

Could you please provide me with a little bit more information about your scenario?
Thank you in advance.

Regards,
Nedyalko Nikolov
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get now >>

Tags
GridView
Asked by
Matthew Kruczek
Top achievements
Rank 1
Answers by
Rossen Hristov
Telerik team
Matthew Kruczek
Top achievements
Rank 1
Nedyalko Nikolov
Telerik team
Share this question
or