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
close icon

How sync the order of the itemssource-collection with the view

Hi,

I am trying to work with the SfThreeGrid. Which is really a great control.

For the itemsource I use an ObservableCollection<>.

While drag-and-dropping the items to restructure them, I notice that items are not restructured in the ObservableCollection.

How do I accomplish a full sync of the order/structure of the itemssource and the view?

Basically I need the node-order to be reflected back into the itemssource collection.

Please advise.

Best regards,

5 Replies

DY Deivaselvan Y Syncfusion Team November 5, 2018 06:42 PM UTC

Hi Johannes,

Thank you for contacting Syncfusion support.

We have analyzed your query “To reorder itemsSource collection based on the visual order after drag and drop” and you can achieve your requirement by handling RowDragDropController.Dropped event as like below code snippet 

 

AssociatedObject.RowDragDropController.Dropped += sfGrid_Dropped; 

 

private void sfGrid_Dropped(object sender, TreeGridRowDroppedEventArgs e)

{

    if (e.DropPosition != DropPosition.None)

    {

        //Get Dragging nodes

        ObservableCollection<TreeNode> draggingRecords = e.Data.GetData("Nodes") as ObservableCollection<TreeNode>;

        if (draggingRecords == null)

            return;

        ViewModel model = AssociatedObject.DataContext as ViewModel;

        //Use Batch update to avoid data operations in SfTreeGrid during records removing and inserting

        AssociatedObject.BeginInit();

        //Removes the dragging nodes from the underlying collection

        foreach (TreeNode item in draggingRecords)

        {

            model.Employees.Remove(item.Item as EmployeeInfo);

        }

        //Find the target node index after removing the nodes

        int targetIndex = model.Employees.IndexOf(e.TargetNode.Item as EmployeeInfo);

        int insertionIndex = e.DropPosition == DropPosition.DropAbove ? targetIndex : targetIndex + 1;

        insertionIndex = insertionIndex < 0 ? 0 : insertionIndex;

        //Insert dragging nodes to the target position

        for (int i = draggingRecords.Count - 1; i >= 0; i--)

        {

            model.Employees.Insert(insertionIndex, draggingRecords[i].Item as EmployeeInfo);

        }

        AssociatedObject.EndInit();

    }

}

 

Please find sample for the same from below link and let us know if this helps you. 

http://www.syncfusion.com/downloads/support/forum/140723/ze/DragAndDropDemo_1270892076.zip

Regards,

Deivaselvan



MR MrHessellund November 5, 2018 09:15 PM UTC

Hi,

Thank you for this. I will try it out soon.

However, does this not presume the list to be sorted correctly as a starting point?

The data in itemssource might not sorted directly as visualized. I.e. two root-level nodes cn be at the beginning while the child-nodes add up in different order afterwards. This would still be visualized correct, but the itemssource would not updated to reflect to visual order. Also the drag-and-drop solution might not bring the full visual order to the itemssource, but also place the dragged-and-dropped items to the correct position in itemssource. Correct?

Would it be possible to somehow make dump of full nodes list, get the item and use it to reorder the itemssource?

Best regards,



DY Deivaselvan Y Syncfusion Team November 7, 2018 03:11 AM UTC

Hi Johannes,

This given solution will update ItemsSource collection with same order of dropping nodes and it doesn’t update the ItemsSource with the sorting order. You can get the Sorting order of the Collection from SfTreeGrid.View.Records.

Regards,
Deivaselvan
 



MI Mike-E May 12, 2022 04:27 PM UTC

I also ran into this problem with the SfListBox and ended up making my own observable collection/list:

https://github.com/DragonSpark/Framework/blob/master/DragonSpark.Application/Model/Sequences/ObservedList.cs



VS Vijayarasan Sivanandham Syncfusion Team May 13, 2022 06:47 AM UTC

Hi Mike-E,

We are happy to hear that the problem has been resolved by yourself. Please let us know if you have any other concerns. As always, we will gladly assist you😊.

Regards,

Vijayarasan S


Loader.
Live Chat Icon For mobile
Up arrow icon