We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Custum DataAdaptor grouping

Hello,
I want to do pagination / filtering using my service so I'm using a custom adaptor to get all my information. Right now my custom adaptor is very much based on your example with the exception that it uses a service to get the information. My custom adaptor looks like the following:

     public class CraftAdaptor : DataAdaptor
    {
        private readonly ICraftService _craftService;

        public CraftAdaptor(ICraftService craftService)
        {
            _craftService = craftService;
        }

        public IEnumerable<Craft> Crafts { get; set; }

        public async override Task<object> ReadAsync(DataManagerRequest dm, string key = null)
        {
            IEnumerable<Craft> DataSource = await _craftService.GetCrafts(string.Empty); 
            if (dm.Search != null && dm.Search.Count > 0)
            {
                // Searching
                DataSource = DataOperations.PerformSearching(DataSource, dm.Search);
            }
            if (dm.Sorted != null && dm.Sorted.Count > 0)
            {
                // Sorting
                DataSource = DataOperations.PerformSorting(DataSource, dm.Sorted);
            }
            if (dm.Where != null && dm.Where.Count > 0)
            {
                // Filtering
                DataSource = DataOperations.PerformFiltering(DataSource, dm.Where, dm.Where[0].Operator);
            }
            int count = DataSource.Cast<Craft>().Count();
            if (dm.Skip != 0)
            {
                //Paging
                DataSource = DataOperations.PerformSkip(DataSource, dm.Skip);
            }
            if (dm.Take != 0)
            {
                DataSource = DataOperations.PerformTake(DataSource, dm.Take);
            }
            return dm.RequiresCounts ? new DataResult() { Result = DataSource, Count = count } : (object)DataSource;
        }
    }


for my grid, I have the following
          <EjsGrid TValue="Craft" AllowGrouping="true" AllowPaging="true" Height="100%" Width="100%" AllowFiltering="true">
                <EjsDataManager AdaptorInstance="@typeof(CraftAdaptor)" Adaptor="Adaptors.CustomAdaptor"></EjsDataManager>
                <GridPageSettings PageCount="3" PageSizes="true"></GridPageSettings>
                @*<GridGroupSettings ShowDropArea="@BelongsToAdministratorOrganization" Columns="@GroupedColumns"></GridGroupSettings>*@
                <GridColumns>
                    <GridColumn Field=@nameof(Craft.OrganizationName) HeaderText="Organization" ></GridColumn>
                    <GridColumn Field=@nameof(Craft.Name) HeaderText="Name" AllowGrouping="false"></GridColumn>
                    <GridColumn Field=@nameof(Craft.NominalAltitude) HeaderText="Nominal Altitude" Format="N0" Type="ColumnType.Number"                          AllowGrouping="false"></GridColumn>
                    <GridColumn Field=@nameof(Craft.FieldOfView) HeaderText="Field of View" Format="N0" AllowGrouping="false"></GridColumn>
                    <GridColumn Field=@nameof(Craft.CraftType) HeaderText="Type" AllowGrouping="false"></GridColumn>
                </GridColumns>
            </EjsGrid>


The initial load is working and I can see the data showing on the grid. The problem comes when I try to group my  information by a column. In my case, I only allow grouping for OganizationName, When I drag the column to the drop space, I get an exception and not really understand what the problem is. The exception is the following: 
Unhandled exception rendering component: Unable to cast object of type 'Domain.Models.Craft' to type 'Syncfusion.EJ2.Blazor.Data.Group`1[Domain.Models.Craft]'.
System.InvalidCastException: Unable to cast object of type 'Domain.Models.Craft' to type 'Syncfusion.EJ2.Blazor.Data.Group`1[Domain.Models.Craft]'.
   at Syncfusion.EJ2.Blazor.Grids.Internal.GroupModelGenerator`1.GenerateRows(IEnumerable data, Int32 startIndex)
   at Syncfusion.EJ2.Blazor.Grids.Internal.GridContent`1.OnParametersSet()
   at Microsoft.AspNetCore.Components.ComponentBase.CallOnParametersSetAsync()
   at Microsoft.AspNetCore.Components.ComponentBase.SetParametersAsync(ParameterView parameters)
   at Microsoft.AspNetCore.Components.Rendering.ComponentState.SetDirectParameters(ParameterView parameters)
   at Microsoft.AspNetCore.Components.RenderTree.RenderTreeDiffBuilder.UpdateRetainedChildComponent(DiffContext& diffContext, Int32 oldComponentIndex, Int32 newComponentIndex)
   at Microsoft.AspNetCore.Components.RenderTree.RenderTreeDiffBuilder.AppendDiffEntriesForFramesWithSameSequence(DiffContext& diffContext, Int32 oldFrameIndex, Int32 newFrameIndex)
   at Microsoft.AspNetCore.Components.RenderTree.RenderTreeDiffBuilder.AppendDiffEntriesForRange(DiffContext& diffContext, Int32 oldStartIndex, Int32 oldEndIndexExcl, Int32 newStartIndex, Int32 newEndIndexExcl)
   at Microsoft.AspNetCore.Components.RenderTree.RenderTreeDiffBuilder.AppendDiffEntriesForFramesWithSameSequence(DiffContext& diffContext, Int32 oldFrameIndex, Int32 newFrameIndex)
   at Microsoft.AspNetCore.Components.RenderTree.RenderTreeDiffBuilder.AppendDiffEntriesForRange(DiffContext& diffContext, Int32 oldStartIndex, Int32 oldEndIndexExcl, Int32 newStartIndex, Int32 newEndIndexExcl)
   at Microsoft.AspNetCore.Components.RenderTree.RenderTreeDiffBuilder.AppendDiffEntriesForFramesWithSameSequence(DiffContext& diffContext, Int32 oldFrameIndex, Int32 newFrameIndex)
   at Microsoft.AspNetCore.Components.RenderTree.RenderTreeDiffBuilder.AppendDiffEntriesForRange(DiffContext& diffContext, Int32 oldStartIndex, Int32 oldEndIndexExcl, Int32 newStartIndex, Int32 newEndIndexExcl)
   at Microsoft.AspNetCore.Components.RenderTree.RenderTreeDiffBuilder.AppendDiffEntriesForFramesWithSameSequence(DiffContext& diffContext, Int32 oldFrameIndex, Int32 newFrameIndex)
   at Microsoft.AspNetCore.Components.RenderTree.RenderTreeDiffBuilder.AppendDiffEntriesForRange(DiffContext& diffContext, Int32 oldStartIndex, Int32 oldEndIndexExcl, Int32 newStartIndex, Int32 newEndIndexExcl)
   at Microsoft.AspNetCore.Components.RenderTree.RenderTreeDiffBuilder.ComputeDiff(Renderer renderer, RenderBatchBuilder batchBuilder, Int32 componentId, ArrayRange`1 oldTree, ArrayRange`1 newTree)
   at Microsoft.AspNetCore.Components.Rendering.ComponentState.RenderIntoBatch(RenderBatchBuilder batchBuilder, RenderFragment renderFragment)
   at Microsoft.AspNetCore.Components.RenderTree.Renderer.RenderInExistingBatch(RenderQueueEntry renderQueueEntry)
   at Microsoft.AspNetCore.Components.RenderTree.Renderer.ProcessRenderQueue()
fail: Microsoft.AspNetCore.Components.Server.Circuits.CircuitHost[111]

Any help is appreciated

thank you


3 Replies

RS Renjith Singh Rajendran Syncfusion Team January 29, 2020 01:03 PM UTC

Hi Rodrigo, 

Thanks for contacting Syncfusion support. 

We suggest you to handle the Grouping by adding the below codes to perform Grouping with a CustomAdaptor Grid. We have prepared a sample based on this requirement. Please download the sample form the link below, 

 
    public class CraftAdaptor : DataAdaptor 
    { 
        public async override Task<object> ReadAsync(DataManagerRequest dm, string key = null) 
        { 
            OrderService _craftService = new OrderService(); 
            IEnumerable DataSource = await _craftService.GetOrdersAsync(); 
            DataResult DataObject = new DataResult(); 
            ... 
           if (dm.Take != 0) 
            { 
                DataSource = DataOperations.PerformTake(DataSource, dm.Take); 
            } 
            if (dm.Group != null) 
            { 
              foreach (var group in dm.Group) 
              { 
                 DataSource = DataUtil.Group<Orders>(DataSource, group, dm.Aggregates, 0, dm.GroupByFormatter); 
              } 
              DataObject.Result = DataSource; 
              DataObject.Count = count; 
              return dm.RequiresCounts ? DataObject : (object)DataSource; 
           } 
            return dm.RequiresCounts ? new DataResult() { Result = DataSource.ToList(), Count = count } : (object)DataSource; 
        } 
    } 


We will document this topic and this will be refreshed online in our upcoming releases. Please get back to us if you need further assistance. 

Regards, 
Renjith Singh Rajendran. 



RO Rodrigo February 5, 2020 10:51 AM UTC

This resolved the Issue! thank you fort he support


RS Renjith Singh Rajendran Syncfusion Team February 5, 2020 12:23 PM UTC

Hi Rodrigo, 

Thanks for your update. 

We are glad to hear that the provided solution helped you in achieving your requirement. 

Please get back to us if you need further assistance. 

Regards, 
Renjith Singh Rajendran. 


Loader.
Live Chat Icon For mobile
Up arrow icon