CustomAdapter - Filtering with page variables

Hi,

I am trying to perform filtering using DataAdaptor by creating some variables in the code section and using their values in the linq method that populates the collection (see code below).  I get a compiler error "Error CS0120 An object reference is required for the non-static field, method, or property", indicating that the customadaptor, being a nested class, does not have access to the page variable (year and month).

I don't think I can define an overloaded constructor for custom adaptor to pass in a reference.  How can I make this work?

Bob


@code{

 int year;
 int month;

 protected override void OnInitialized()
    {
        year = 2020;
        month = 2;
    }

    public class CustomAdaptor : DataAdaptor
    {
        public InstallmentsDataServiceService data = new InstallmentsDataServiceService();

        public override object Read(DataManagerRequest dm, string key = null)
        {
            IEnumerable<Installment> DataSource = data.GetInstallmentsByYearMonth(year, month).ToList();
            if (dm.Search != null && dm.Search.Count > 0)
            {
                // Searching
                DataSource = DataOperations.PerformSearching(DataSource, dm.Search);
            }
            if (dm.Sorted != null && dm.Sorted.Count > 0)
            {
                // Sorting......etc.....

5 Replies

VN Vignesh Natarajan Syncfusion Team February 26, 2020 06:20 AM UTC

Hi Bob,  
 
Thanks for contacting Syncfusion forums.  
 
Query: “I don't think I can define an overloaded constructor for custom adaptor to pass in a reference.  How can I make this work? 
 
From your code example we found that you are trying to load the dataSource from service (database). So we suggest you to achieve your requirement by using CustomAdaptor as component. Kindly refer our UG documentation for your reference 
 
 
Kindly get back to us if you have further queries.  
 
Regards, 
Vignesh Natarajan.  



BT Bob Troppmann February 26, 2020 06:18 PM UTC

Thanks for the response.  

The code I posted came from code generated by the Syncfusion scaffolder (Syncfusion Blazor extensions for Visual Studio).

I think the component based approach will work, but I am having some trouble following the code on the documentation page.  

For example in the section below I think the CascasingValue tag should be in the  parent component not the child component.
Alos, the services declaration is ambiguous.

Is it possible for you to post a sample implementation of this?

Thanks

Bob

CustomAdaptorComponent.razor]

@using Syncfusion.EJ2.Blazor;
@using Syncfusion.EJ2.Blazor.Data;
@using static BlazorApp1.Pages.Index
@using Newtonsoft.Json

@inherits DataAdaptor

<CascadingValue Value="@this">
    @ChildContent
</CascadingValue>


VN Vignesh Natarajan Syncfusion Team February 27, 2020 10:23 AM UTC

Hi Bob,  
 
Thanks for the update.  
 
Query: “Is it possible for you to post a sample implementation of this? 
 
As per your request we have prepared a sample using the concept explained in the CustomAdaptor as Component documentation using our latest version Nuget package 17.4.0.51. Kindly download the sample from below  
 
 
Kindly get back to us if you have further queries.  
 
Regards, 
Vignesh Natarajan.  
 



CP Cory Prause November 26, 2021 06:55 AM UTC

For anyone who comes back to this, in .NET 6 it's even easier to do this if you need it to be a little more generic and pass in the type of Adaptor with optional parameter(s):


@typeparam TRecordType
    <SfGrid>
...
        <SfDataManager Adaptor="Syncfusion.Blazor.Adaptors.CustomAdaptor">
            <DynamicComponent Type="@AdaptorType" Parameters="AdaptorParameters"/>
        </SfDataManager>
....
    </SfGrid>


@code {
    [Parameter, EditorRequired]
    public Type AdaptorType { get; set; }
    [Parameter]
    public Guid? ParentGUID { get; set; }

    private Dictionary<string, object> AdaptorParameters { get; set; }

    protected override void OnInitialized()
    {
        if (ParentGUID.HasValue)
        {
            AdaptorParameters = new Dictionary<string, object>
                {
                    ["ParentGUID"] = ParentGUID
                };
        }
    }
}


VN Vignesh Natarajan Syncfusion Team November 29, 2021 04:40 AM UTC

Hi Cory,  

Thanks for your solution.  

We are glad to hear that you have resolved your query on your own and thanks for sharing your solution. 

Please get back to us if you have further queries.  

Regards, 
Vignesh Natarajan 


Loader.
Up arrow icon