Getting a Datasource for a Custom Sort Implementation

Hello,


I wanted to implement a custom sort in Grid, so I referred to the following forum.

https://www.syncfusion.com/forums/158251/custom-sort-on-grid-column


It was very helpful and I was able to implement the custom sort, but there is one problem that I cannot solve.

In the sample presented in the above forum, there is a scene where the DataSource is retrieved in the Read method.


// Implementing custom adaptor by extending the DataAdaptor class

public class CustomAdaptor : DataAdaptor

{

// Performs data Read operation

public override object Read(DataManagerRequest dm, string key = null)

{

IEnumerable DataSource = Orders;


In this case, Orders, which is assigned as DataSource, is a static member of the outer class.

However, if this is static, won't multiple clients accessing the same page at the same time end up sharing the same value?


I would like to getting Orders as the DataSource is an instance member in the page component, but I don't know how.


Maybe I am making some basic misunderstanding.

Any help would be appreciated.


Regars,

Atsushi


3 Replies

VN Vignesh Natarajan Syncfusion Team September 28, 2021 09:22 AM UTC

Hi Atsushi,  
 
Greetings from Syncfusion support.  
 
Query: “I would like to getting Orders as the DataSource is an instance member in the page component, but I don't know how 
 
We have analyzed your query and we understand that you want to use the Orders property value from component page. Also it must not be defined as static variable. We suggest you to achieve your requirement by defining the CustomAdaptor as separate component and pass the Orders value as  parameter to CustomAdaptor Component.  
 
Refer the below code example.  
 
<SfGrid TValue="Order" ID="Grid" AllowSorting="true" AllowFiltering="true" AllowPaging="true"> 
    <SfDataManager Adaptor="Adaptors.CustomAdaptor"> 
        <AdaptorComponent GridData="Orders"></AdaptorComponent> 
    </SfDataManager> 
    <GridPageSettings PageSize="8"></GridPageSettings> 
    <GridColumns> 
        <GridColumn Field=@nameof(Order.OrderID) HeaderText="Order ID" IsPrimaryKey="true" TextAlign="TextAlign.Right" Width="120"></GridColumn> 
        <GridColumn Field=@nameof(Order.Alphanumeric) HeaderText="Alpha numeric" Width="120"></GridColumn> 
        <GridColumn Field=@nameof(Order.Numeric) HeaderText="Numeric" Width="120"></GridColumn> 
        <GridColumn Field=@nameof(Order.OrderDate) HeaderText=" Order Date" Format="d" TextAlign="TextAlign.Right" Width="130" Type="ColumnType.Date"></GridColumn> 
        <GridColumn Field=@nameof(Order.Freight) HeaderText="Freight" Format="C2" TextAlign="TextAlign.Right" MinWidth="10" Width="120" MaxWidth="200"></GridColumn> 
    </GridColumns> 
</SfGrid> 
  
@code{ 
    public List<Order> Orders { getset; } 
  
    protected override void OnInitialized() 
    { 
        Orders = Enumerable.Range(0, 4).Select(x => new Order() 
        { 
            OrderID = 1000 + x, 
            Alphanumeric = (new string[] { "ToE1""ToE2""ToE13""ToE14" })[x], 
            Numeric = x, 
            Freight = 2.1 * x, 
            OrderDate = DateTime.Now.AddDays(-x), 
        }).ToList(); 
    } 
 
 
[AdaptorComponent.razor] 
 
@code {    [Parameter]    [JsonIgnore]    public RenderFragment ChildContent { getset; }     [Parameter]    public List<Order> GridData { getset; }     // Performs data Read operation    public override object Read(DataManagerRequest dm, string key = null)    {        IEnumerable<Order> DataSource = (IEnumerable<Order>)GridData;. . . .         return dm.RequiresCounts ? new DataResult() { Result = DataSource, Count = count } : (object)DataSource;    }
 
 
Kindly refer the below sample for your reference 
 
 
Refer our UG documentation for your reference 
 
 
Please get back to us if you have further queries or if we misunderstood your requirement.  
 
Regards, 
Vignesh Natarajan 



AS Atsushi Sugimoto September 29, 2021 06:04 AM UTC

Hi Vignesh,


It worked fine!

Thank you for all your help.


Regards, 
Atsushi.


RS Renjith Singh Rajendran Syncfusion Team September 30, 2021 04:30 AM UTC

Hi Atsushi, 

Thanks for your update. Please get back to us if you need further assistance. 

Regards, 
Renjith R 


Loader.
Up arrow icon