Using Radgrid row drop to change sort order in database

1 Answer 22 Views
Grid
Anthony
Top achievements
Rank 1
Anthony asked on 14 Dec 2021, 06:45 PM

Hi,

I'm trying to use the RowDrop method of the Radgrid to change the sort order in the database.  Each row in the database table has a "SortOrder" column to indicate the order in which items should appear. 

I want to allow the user to quickly change these orders.

Do you have any code samples that could easily do this?  The example provided online doesn't seem to address this.

Thank you!

 

1 Answer, 1 is accepted

Sort by
0
Attila Antal
Telerik team
answered on 17 Dec 2021, 06:17 PM

Hi Anthony,

I have already answered your Support ticket but I will share the answer in this forum thread as well to make it available to the Community.


Updating the SortOrder is not part of the Drag and Drop functionality. You will need to implement that additionally.

If you think about it, the whole Drag and Drop functionality consists of a single event that is triggered when Dropping a row. Everything in the event handler is a custom logic that uses the information from the event arguments to be able to move records around in the database table. Updating the SortOrder will be an additional logic.

Here is an example of this concept:

protected void PlanSortGrid_RowDrop(object sender, GridDragDropEventArgs e)
{
    // Custom Logic to move items around in the Collection (database)

    // Custom Logic to Update the SortOrder
      
    // Custom Logic to Fetch the Updated records and assign it to RadGrid

    // Rebind the Grid
    PlanSortGrid.Rebind();
}

 

If I had to update the SortOrder, the first thing that comes to my mind would be to loop through the list of items (after reordering) and reset the SortOrder for each. 

for (int i = 0; i < pendingBenefitPlans.Count(); i++)
{
    var sortOrder = i + 1;

    var planToUpdate = pendingBenefitPlans[i];

    planToUpdate.SortOrder = sortOrder;
}

 

Of course, this is just one approach. If you know a better way to do that, feel free to change it.


Once all items contain the new information you will assign the collection back to RadGrid and call the Rebind, thus refreshing the data on the Page.

 

I have attached a Visual Studio sample that is demonstrating this scenario.

 

Regards,
Attila Antal
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Tags
Grid
Asked by
Anthony
Top achievements
Rank 1
Answers by
Attila Antal
Telerik team
Share this question
or