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

Binding with Entity Framework

4 Answers 208 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Dave
Top achievements
Rank 1
Dave asked on 28 Jan 2014, 04:22 PM
I'm trying to follow the demo shown here but when I run it as is I receive the following:

[NotSupportedException: Data binding directly to a store query (DbSet, DbQuery, DbSqlQuery, DbRawSqlQuery) is not supported. Instead populate a DbSet with data, for example by calling Load on the DbSet, and then bind to local data. For WPF bind to DbSet.Local. For WinForms bind to DbSet.Local.ToBindingList().]
   System.Data.Entity.Infrastructure.DbQuery`1.System.ComponentModel.IListSource.GetList() +44
   System.Web.UI.DataSourceHelper.GetResolvedDataSource(Object dataSource, String dataMember) +57
   System.Web.UI.WebControls.ReadOnlyDataSource.System.Web.UI.IDataSource.GetView(String viewName) +42
   System.Web.UI.WebControls.DataBoundControl.ConnectToDataSourceView() +145
   System.Web.UI.WebControls.DataBoundControl.GetData() +4
   Telerik.Web.UI.RadGrid.GetData() +132
   System.Web.UI.WebControls.DataBoundControl.PerformSelect() +54
   System.Web.UI.WebControls.BaseDataBoundControl.DataBind() +30
   Telerik.Web.UI.RadGrid.DataBind() +107
   Telerik.Web.UI.RadGrid.AutoDataBind(GridRebindReason rebindReason) +4252
   Telerik.Web.UI.RadGrid.OnLoad(EventArgs e) +201
   System.Web.UI.Control.LoadRecursive() +54
   System.Web.UI.Control.LoadRecursive() +145
   System.Web.UI.Control.LoadRecursive() +145
   System.Web.UI.Control.LoadRecursive() +145
   System.Web.UI.Control.LoadRecursive() +145
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +772

However, if I just select a subset and call ToList() on it, it doesn't throw the error.

Is there a new demo that shows the latest way of binding with EF? I will be using all the paging, filtering, sorting and grouping features.

Regards.

4 Answers, 1 is accepted

Sort by
0
Dave
Top achievements
Rank 1
answered on 28 Jan 2014, 07:53 PM
I've changed the grid to instead use an EntityDataSource and things seem to be working alright now. The paging, sorting, filtering work quite impressively.

But now I'm running into a new issue. I would like to be able to have the user make changes, then when they save these changes I will manually do the updating through a separate connection that contains all of the logic.

Here is how I currently have things setup:
<telerik:RadGrid
    ID="RadGridPrototype"
    runat="server"
    AutoGenerateColumns="false"
    AllowSorting="True"
    AllowPaging="True"
    AllowFilteringByColumn="True"
    MasterTableView-AllowMultiColumnSorting="true"
    GridLines="none"
    PageSize="25"
    ShowGroupPanel="true"
    EnableHeaderContextMenu="true"
    EnableHeaderContextFilterMenu="true"
    DataSourceID="EntityDataSource1"
    OnItemDataBound="RadGridPrototype_ItemDataBound"
    OnUpdateCommand="RadGridPrototype_UpdateCommand">
    <MasterTableView DataKeyNames="ID" CommandItemDisplay="Bottom" EditMode="Batch">
        <CommandItemSettings ShowAddNewRecordButton="false" ShowExportToExcelButton="true" />
        <BatchEditingSettings EditType="Cell" OpenEditingEvent="DblClick" />
        <PagerStyle Mode="NextPrevAndNumeric" PagerTextFormat="{4} Page {0} of {1} | Displaying rows {2}-{3} of {5}" />
    </MasterTableView>
    <ClientSettings AllowDragToGroup="true">
    </ClientSettings>
</telerik:RadGrid>
 
<asp:EntityDataSource
    ID="EntityDataSource1"
    runat="server"
    ConnectionString="name=TicketContext"
    DefaultContainerName="TicketContext"
    EntitySetName="Ticket_NonFiltered"
    OnUpdating="EntityDataSourceTickets_Updating" />

Then in the code behind at the RadGridPrototype_UpdateCommand function:

protected void RadGridPrototype_UpdateCommand(object sender, GridCommandEventArgs e)
{
    var editedItem = ((GridEditableItem)e.Item);
 
    string rowID = (string)editedItem.GetDataKeyValue("ID");
 
    //Option 1
    using (DBItemContext context = new DBItemContext())
    {
        var rowValues = context.NeededTable.Where(n => n.Id == rowID).FirstOrDefault();
        if (rowValues != null)
        {
            // You can actually make changes here to the entity:
            rowValues.Field1 = "garbage"; // this will be returned to original value
 
            // This call restores all the original values, none of the edited values from the grid come in.
            editedItem.UpdateValues(rowValues);
        }
    }
 
    //Option 2
    Hashtable newValues = new Hashtable();
    editedItem.OwnerTableView.ExtractValuesFromItem(newValues, editedItem);
    // The newValues hash has all the row values, but none of them are the edited ones from the grid.
}


The page posts back before calling the UpdateCommand method, so the values I retrieve aren't actually the changed ones and both of the methods above of trying to get the edited values just return the originals.

What should my approach here be? I'm assuming going back to having the advanced binding could possibly fix it, but when using the NeedDataSource event since you have to call ToList() to set the source there's no automatic paging and the entire set was being retrieved each time which takes much too long.

 

0
Accepted
Shinu
Top achievements
Rank 2
answered on 29 Jan 2014, 08:54 AM
Hi Dave,

You can handle the Batch edit operations in the OnBatchEditCommand event of the RadGrid. Please try the following code snippet:

C#:
protected void RadGridPrototype_BatchEditCommand(object sender, GridBatchEditingEventArgs e)
{
 foreach (GridBatchEditingCommand command in e.Commands)
  {
   if ((command.Type == GridBatchEditingCommandType.Update)) // For Update
    {
      Hashtable newValues = command.NewValues;            
      string ID= newValues["ID"].ToString();              
    }
  }
}

Thanks,
Shinu
0
Dave
Top achievements
Rank 1
answered on 29 Jan 2014, 04:08 PM
Hi Shinu,

That seems to work great, thank you!

I also noticed that the same thing can be handled in the ItemCommand event:

protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
{
    if (e.CommandName == RadGrid.UpdateCommandName)
    {
        Hashtable updatedRowValues = (e.CommandArgument as GridBatchEditingEventArgument).NewValues;
        string ID = updatedRowValues["ID"].ToString();
    }
}

Would there be a reason to prefer one over the other?

Thank you.
0
Shinu
Top achievements
Rank 2
answered on 30 Jan 2014, 03:06 AM
Hi Dave,

Please go through the Server-side API topic in this documentation on Batch Editing. It discuss the use of different events.

Thanks,
Shinu
Tags
Grid
Asked by
Dave
Top achievements
Rank 1
Answers by
Dave
Top achievements
Rank 1
Shinu
Top achievements
Rank 2
Share this question
or