ToDataSourceResult with specific record first

1 Answer 273 Views
Data Query Grid
Grahame
Top achievements
Rank 1
Iron
Grahame asked on 12 Jul 2021, 11:31 AM | edited on 12 Jul 2021, 02:00 PM

Hi,

We have a grid which displays a dialog for the user to add a record. Once it's been added the webapi (c#) returns the new record id and the dialog closes.

How can I get the grid to be reloaded with the new record as the first row and still honour the sorts/filters and still use DataSourceRequest and ToDataSourceResult() ?

I think I'm after a UNION but with the new record first.

Cheers,

Grahame

1 Answer, 1 is accepted

Sort by
0
Accepted
Grahame
Top achievements
Rank 1
Iron
answered on 14 Jul 2021, 01:33 PM

Just to answer my own question so anyone can find it.

Svet from Telerik said the ToDataSourceResult() is applied after getting the initial record set.

This Stackoverflow article told me how to put a row to the beginning of the list.

Here is my completed C# webapi endpoint:

// GET: api/OrderHeader
[HttpGet] 
public DataSourceResult Get([DataSourceRequest] DataSourceRequest request, int userId, int sessionId, int newOrderReference = -23)
{
            using var db = _context;

            AuthorisationHelpers.UpdateSession(userId, sessionId);

            var data = (from x in db.Order_Header
                        orderby x.Unique_Id
                        select x).ToList();

            // Move new order reference to top of list?
            if (newOrderReference != -23)
            {
                // Taken from: https://stackoverflow.com/questions/1668451/use-linq-to-move-item-to-top-of-list
                var index = data.FindIndex(x => x.Order_Reference == newOrderReference);
                var item = data[index];
                data[index] = data[0];
                data[0] = item;
            }

            var results = data.ToDataSourceResult(request);

            return results;
}

Cheers,

Grahame

Tags
Data Query Grid
Asked by
Grahame
Top achievements
Rank 1
Iron
Answers by
Grahame
Top achievements
Rank 1
Iron
Share this question
or