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

Passing through the TValue of an SFGrid to the Datamanager in a custom data adaptor

Good day

I currently have an app that I need to pass in the TValue from the SFGrid to the datamanager.

My TValues are models and can vary e.g. MaterialModel, ProductModel etc.

Currently I can use the data object in the update method to dynamically read the Model type (MaterialModel) and then it works correctly with my Data access class. 

My issue is with the ReadAsync method, as I cannot pass in the TValue or any reference to the type of model that is being read.

Do you perhaps have a solution as Datamanager requires the TValue to be set therefore that TValue should be able to be accessed and passed in another method in the customDataAdaptor.


using Syncfusion.Blazor.Data;

using System.ComponentModel.DataAnnotations;

See snippet of code below.

namespace MRPUI.DataAdaptors;

    public class CustomDataAdaptor : DataAdaptor

    {

    private readonly IDataSP _dataSP;


    public CustomDataAdaptor(IDataSP dataSP)

    {

        _dataSP = dataSP;

    }

    public override async Task<object> ReadAsync(DataManagerRequest dm, string key = null)

    {


            var customModel = await _dataSP.GetAllData <TValue>();

            int finalcount = customModel.Count();


            if (dm.Search != null && dm.Search.Count > 0)

            {

                // Searching

                customerModel = DataOperations.PerformSearching(customModel, dm.Search);

            }

            if (dm.Sorted != null && dm.Sorted.Count > 0)

            {

                // Sorting

                customerModel = DataOperations.PerformSorting(customModel, dm.Sorted);

            }

            if (dm.Where != null && dm.Where.Count > 0)

            {

                // Filtering

                customerModel = DataOperations.PerformFiltering(customerModel, dm.Where, dm.Where[0].Operator);

            }

            if (dm.Skip != 0)

            {

                //Paging

                customerModel = DataOperations.PerformSkip(customerModel, dm.Skip);

            }

            if (dm.Take != 0)

            {

                customModel = DataOperations.PerformTake(customModel, dm.Take);

            }

            return dm.RequiresCounts ? new DataResult() { Result = customModel, Count = finalcount } : (object)customModel.ToList();

        }

        public override async Task<object> InsertAsync(DataManager dm, object data, string key)

        {

            await _dataSP.InsertData(data as dynamic);

            return data;

        }

        public override async Task<object> UpdateAsync(DataManager dm, object data, string keyField, string key)

        {

            await _dataSP.UpdateData(data as dynamic);

            return data;

        }


1 Reply 1 reply marked as answer

BL Balamurugan Lakshmanan Syncfusion Team January 25, 2023 08:30 AM UTC

Hi Michael,


We have analysed the query and we would like to inform that you can use the type parameter T to define the type of TValue in the custom data adaptor class and use it in both the ReadAsync and Update methods. This way, you can pass in the specific type of model (MaterialModel or ProductModel) when creating an instance of the custom data adaptor class and use it in the data access class.

Another option is to create a property in the custom data adaptor class and set the TValue from the SFGrid in the constructor. Then you can access the property in the ReadAsync and Update methods.

In either case, you'll need to make sure that the custom data adaptor implements the IDataAdaptor interface and meets the requirements of the DataManager's ReadAsync and Update methods.


Please get back to us if you  have any concern.


Regards,

Balamurugan L


Marked as answer
Loader.
Live Chat Icon For mobile
Up arrow icon