How to control search after adding data row in DataGrid on insert of new row

I have a CustomAdaptor that implements Insert, Update, Remove and Read to a back-end repository.

When I add a new row in a DataGrid that is bound to a DataManager that references my CustomAdaptor, the newly added data is persisted properly however I need to move to the last page in the updated dataset in order to see the newly added row.

Example:  

If I have 2 pages worth of data, when I add a new row while on page 1, the added row is persisted to the back-end properly,  however when the DataManager performs its final Read after the Insert (via the CustomAdaptor), the current page remains on page 1,  however the row added is located on page 2 and in order to see that new row I have to page forward until the last page. 

Note: The behavior is the same when I turn off sorting.  

Ideally after adding a record,  I would like to move to the last page as to where that newly created record is located on.    

What is the best way to handle this?      



3 Replies 1 reply marked as answer

RS Renjith Singh Rajendran Syncfusion Team April 20, 2021 11:53 AM UTC

Hi John, 

Greetings from Syncfusion support. 

Query : Ideally after adding a record,  I would like to move to the last page as to where that newly created record is located on. 
We suggest you to use the GoToPage method to Grid in OnActionComplete event to navigate to the corresponding page after adding a new record to Grid. Please refer and use the codes below, 

 
<GridEvents OnActionComplete="OnActionComplete" TValue="Order"></GridEvents> 
public static List<Order> Orders { getset; }...public async Task OnActionComplete(ActionEventArgs<Order> args){    if(args.RequestType.Equals(Action.Save) && args.Action == "Add")    {        var TotalPageCount = Orders.Count() / Grid.PageSettings.PageSize;        await Grid.GoToPage(TotalPageCount + 1);    }}

We have also prepared a sample for your convenience. Please download the sample from the link below, 

References :  

Please get back to us if you need further assistance. 

Regards, 
Renjith R 


Marked as answer

JO John April 20, 2021 12:32 PM UTC

Thank you for this novel solution, very much appreciated.

I was thinking that if other clients were also adding/editing data to the same underlying data source then the page may not be the last one, or if existing sorting was on then it may not be the last page.

As per the custom adapter design, the added item along with its new id of the added data item is returned to the datamanager.  

Is there a way of passing this new id insight from the insert back to data manager which is then forwarded into the final read such that we can utilize this insight to recalculate the page index on the server side in order to return the page containing the newly added item with respect to that final read request. 

Ideally if there was a flag on the DMR that could be set on exit of insert and read on the final read, that would be enough insight to allow me to query the back end.

In otherwords when the Final Read is performed, it is passed the id of the data item newly added, reestablishing the current page index.  

I feel this would be more efficient as I believe your solution would perform another read post the final read after an insert and would not gaurantee that the page returned has the newly added item on it.

Cheers



RS Renjith Singh Rajendran Syncfusion Team April 21, 2021 12:23 PM UTC

Hi John, 

Based on this scenario, we suggest you to calculate the page number where the newly added row is saved inside the CustomAdaptor class. And use this calculated PageNumber as parameter for the GoToPage method to make the Grid show the proper page where the newly added row is saved. 

We have also prepared a sample based on this requirement. Please download the sample from the link below, 
 
 
<GridEvents OnActionComplete="OnActionComplete" TValue="Order"></GridEvents>
 
public async Task OnActionComplete(ActionEventArgs<Order> args) 
{ 
    if(args.RequestType.Equals(Action.Save) && args.Action == "Add") 
    { 
        await Grid.GoToPage(PageNumber + 1); 
    } 
} 
... 
public static double PageNumber { getset; } 
public class CustomAdaptor : DataAdaptor 
{ 
    public bool flag = false; 
    public Order PrimarkKeyValue { getset; } 
    public override object Read(DataManagerRequest dm, string key = null) 
    { 
        IEnumerable<Order> DataSource = Orders; 
        ... 
        if (dm.Sorted != null && dm.Sorted.Count > 0) 
        { 
            // Sorting 
            DataSource = DataOperations.PerformSorting(DataSource, dm.Sorted); 
        } 
        if (flag) 
        { 
            var index = DataSource.IndexOf<Order>(PrimarkKeyValue); 
            PageNumber = index / dm.Take; 
            flag = false; 
        } 
        ... 
        return dm.RequiresCounts ? new DataResult() { Result = DataSource, Count = count } : (object)DataSource; 
    } 
 
    // Performs Insert operation 
    public override object Insert(DataManager dm, object value, string key) 
    { 
        flag = true; 
        PrimarkKeyValue = (value as Order); 
        Orders.Insert(Orders.Count(), value as Order); 
        return value; 
    } 
    ... 


Please get back to us if you need further assistance. 

Regards, 
Renjith R 


Loader.
Up arrow icon