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

RadGrid bound to Entity Framework 6.1.1. How To...

5 Answers 187 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Sander
Top achievements
Rank 1
Sander asked on 11 Nov 2014, 01:08 PM
Hi all,

I am struggling with binding some entities from the Entity Framework 6.0 to the RadGrid.
The solution contains an Entity Framework 6.1.1 Code First Migrations project in which I have a DBContext which contains several DBSets. A DBSet is just a set of POCO classes.

Now comes the struggle... How to bind it with a RadGrid inside an ASP.NET project. I already tried some things which I found in the demo`s forums and other internet posts.
I tried it with the EntityDataSource but this one only uses a ObjectContext instead of a DBContext. I know there is a NuGetPackage which contains an updated EntityDataSource and need to test this.
I also tried connecting the DBSet directly to the datasource property of the datagrid by using the NeedDataSource event. In this case I get an error on loading the web page: Data binding directly to a store query (DbSet, DbQuery, DbSqlQuery, DbRawSqlQuery) is not supported......For ASP.NET WebForms you can bind to the result of calling ToList() on the query or use Model Binding.

When I try to do as described in the above error and call the ToList() method on the DBSet the page is rendered correctly but all things like sorting, filtering, paging, etc are not working.

Can someone please help me on this and give me some advice how to bind the RadGrid to the Entity Framework 6.1.1 entities so the sorting, filtering, grouping, paging, etc is working out of the box?
Beside the above functionality I also need CRUD operations. I guess this has to be implemented through code and will not come Out Of the Box?

Thank you all in advance!!!

5 Answers, 1 is accepted

Sort by
0
Sander
Top achievements
Rank 1
answered on 12 Nov 2014, 08:58 AM
I did some more testing on the project and I found an issue with the dynamic creating of the RadGrid. The problem was I added the RadGrid into a Div control which was added to the web page in the CreateChildControls event. When I do this and use Advanced databinding with the NeedDataSource option the data is loaded correctly in the Grid but the paging, sorting, grouping and filtering was not working. Now I create the Div in the OnInit event and now the paging, sorting, etc is working like a charm.
When binding the data in the NeedDataSource the RadGrid is bound to an entity from Entity Framework by using the ToList() method.
Which left is to implement the event for insert, update and delete manually.

The only thing I still wondering about is whether I am using the correct technique for databinding the Entity Framework with the RadGrid. Is Advanced databind with the NeedDataSource event the correct one, or should I use the EntityDataSource control, or maybe something else...

0
Brett
Top achievements
Rank 1
answered on 13 Nov 2014, 09:04 PM
If you're looking for binding context, where the primary mechanism is postbacks, I would recommend using a ODataSource object that maps to a "Service" Class of your own design.

Simply put, you should 4 methods (Get|Update|Insert|Delete) in your service class, which in turn is able to access your repository/dbcontext.

The Get Method should return IEnumerable<EntityPOCO>.

Use the MasterViewTable columns to limit the availability of your pocos.If you decide to go with DTO / ModelViews for your packeting/exchanges then remember that all you need is an IEnumerable of an open object that has public property members.

0
Brett
Top achievements
Rank 1
answered on 13 Nov 2014, 09:06 PM
by "limit the availability of your pocos" I meant the properties of the poco that you don't want to exposed to the grid, or whatever control you're binding to.
0
Brett
Top achievements
Rank 1
answered on 13 Nov 2014, 09:06 PM
by "limit the availability of your pocos" I meant the properties of the poco that you don't want to exposed to the grid, or whatever control you're binding to.
0
Sander
Top achievements
Rank 1
answered on 26 Nov 2014, 02:26 PM
Hi Brett,

Thank you for the response and the tips.
I went for the approach with the NeedDatasource method while this looks better usable in my situation. The situation is that I am creating the grid and the events in a base class and all classes which are inheriting the base class do have to implement the NeedDataSource method.

The paging, filtering, sorting and grouping is also working like a charm. Only 1 issue with filtering on DateTime columns, but still reading and studying on this issue.

Thanks again for your tips and what I am concerned this post can be closed.
Tags
Grid
Asked by
Sander
Top achievements
Rank 1
Answers by
Sander
Top achievements
Rank 1
Brett
Top achievements
Rank 1
Share this question
or