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

Using EntityDataSource, Filtering and automatic operations at the same time.

2 Answers 283 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Ryan O'Neill
Top achievements
Rank 1
Ryan O'Neill asked on 19 Nov 2010, 12:07 PM
I've got a grid with data that can be sorted, paged and filtered by the end user. The grid data is tied to a declarative EntityDataSource and it works very well. I want to filter the data returned by the EntityDataSource so that the user only sees a subset of the records.

OK, so I can't use a declarative EDS and Filters on the grid at the same time as the Filters overwrite the 'Where' clause I put in the EDS. So I'm trying to move the EDS to code behind using the NeedDataSource event.

This is the problem I can see, it seems that the NeedDataSource event expects ALL data to be returned (already bound) which means I'll have lost all of my automatic paging and sorting on the server. I also can't see an example that uses the EDS like this (my code below encounters a NullException error).

My code:
protected void RadGrid1_NeedDataSource(object source, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
{
    var Eds = new EntityDataSource();
 
    Eds.DefaultContainerName = "EntityDb";
    Eds.ConnectionString = "name=EntityDb";
    Eds.EntitySetName = "Meetings";
    Eds.EnableDelete = true;
    Eds.EnableInsert = true;
    Eds.EnableUpdate = true;
    Eds.AutoPage = true;
    Eds.Include = "Venue";
    Eds.OrderBy = "it.StartDate";
    RadGrid1.DataSource = Eds;
}

The code above does not work, the code below (from the demo) looks like paging and sorting are done in the grid, not passed to the datasource which means the benefit of the EDS is lost (only fetching the rows needed etc).

Example from demo:
protected void RadGrid1_NeedDataSource(object source, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
{
 OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + Server.MapPath("~/App_Data/Nwind.mdb"));
 OleDbDataAdapter adapter = new OleDbDataAdapter();
 adapter.SelectCommand = new OleDbCommand("SELECT * FROM Customers", conn);
 DataTable myDataTable = new DataTable();
 conn.Open();
 try
 {
    adapter.Fill(myDataTable);
 }
 finally
 {
    conn.Close();
 }
 RadGrid1.DataSource = myDataTable;
}

Questions;
1) Is there a good sample of using EDS in code behind?
2) Is there another way to accomplish this declaratively?
3) Will the EDS in the code behind always get all of the data and sort it in the grid or does it use the EDS properly?

Thanks in advance,

Ryan

2 Answers, 1 is accepted

Sort by
0
Radoslav
Telerik team
answered on 25 Nov 2010, 08:50 AM
Hello Ryan,

Regarding your first question:
You could check out the following online example which demonstrates Entity Framework Manual CRUD Operations with RadGrid advanced DataBinding:
http://demos.telerik.com/aspnet-ajax/grid/examples/dataediting/entityframeworkcrud/defaultcs.aspx

With respect to your second question:
On the following link you could find an example of RadGrid declaratively bound to data via the EntityDataSource:
http://demos.telerik.com/aspnet-ajax/grid/examples/automaticoperations/efdatabinding/defaultcs.aspx
In this case the filtering, paging, sorting are performed on database level and only the data for the current page is assigned to the RadGrid.
For your convenience I've prepared sample demonstrating RadGrid binding to EntityDataSource with paging/sorting/filter. If you perform one of those operations Label will be updated with Count of the records returned to RadGrid.

Regarding your third question:
In order to assign only the PageSize items to RadGrid when you use advanced DataBinding and NeedDataSource event you need to sort/filter manually the data source and then get the PageSize items manually and assign them to the RadGrid.

I hope this helps.

Best wishes,
Radoslav
the Telerik team
Browse the vast support resources we have to jumpstart your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.
0
Ryan O'Neill
Top achievements
Rank 1
answered on 25 Nov 2010, 11:41 AM
Thanks, in the end I did the filtering by extending the query created by EDS / RadGrid. It's below if anyone wants to do anything similar.

/// <summary>
/// Extends the declarative entity data source query by adding additional filters.
/// Used to hide the venues that are not live.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void EntityDs_QueryCreated(object sender, QueryCreatedEventArgs e)
{
    var gridAutogeneratedQuery = e.Query.Cast<Meeting>();
 
    // Add additional filters to the grid.
    e.Query = from item in gridAutogeneratedQuery
              where item.Hidden == false
              select item;
}
Tags
Grid
Asked by
Ryan O'Neill
Top achievements
Rank 1
Answers by
Radoslav
Telerik team
Ryan O'Neill
Top achievements
Rank 1
Share this question
or