Completed
Last Updated: 25 Sep 2024 10:34 by ADMIN
Release 2024.3.924
Bob
Created on: 12 Aug 2024 09:27
Category: ListView
Type: Bug Report
0
RadListView: ArgumentOutOfRangeException is thrown when drag-drop an item at the last position in unbound mode
In this scenario, the control is populated in unbound mode, and drag-drop operation is enabled. Dragging and dropping an item at the last position will lead to ArgumentOutOfRangeException.
1 comment
ADMIN
Dinko | Tech Support Engineer
Posted on: 12 Aug 2024 09:36

Hi Bob,

Thank you for reporting this. You can use the following approach as a workaround. The approach requires modifying the HandleDragDrop() method of the default ListViewDragDropService:

public Form1()
{
    InitializeComponent();
    this.radListView1.AllowDragDrop = true;
    this.radListView1.ListViewElement.DragDropService = new MyDragDropService(this.lvItems.ListViewElement);
}

public class MyDragDropService : ListViewDragDropService
{
    public MyDragDropService(RadListViewElement owner) : base(owner)
    {

    }
    protected override void HandleDragDrop(RadDropEventArgs e)
    {
        RadElement targetElement = e.HitTarget as RadElement;
        BaseListViewVisualItem targetVisualItem = e.HitTarget as BaseListViewVisualItem;
        BaseListViewElement targetViewElement = e.HitTarget as BaseListViewElement;

        if (targetElement != null)
        {
            Point dropLocation = targetElement.PointToControl(e.DropLocation);
            targetVisualItem = this.Owner.ViewElement.GetVisualItemAt(dropLocation);
        }

        ListViewDataItem targetItem = (targetVisualItem != null) ? targetVisualItem.Data : null;
        bool dropAtEnd = false;

        if (targetItem != null)
        {
            targetItem.Owner.ListSource.BeginUpdate();
            if (this.Owner.ViewElement.ShouldDropAfter(targetVisualItem, e.DropLocation))
            {
                ITraverser<ListViewDataItem> enumerator = this.Owner.ViewElement.Scroller.Traverser.GetEnumerator() as ITraverser<ListViewDataItem>;
                enumerator.Position = targetItem;
                dropAtEnd = !enumerator.MoveNext();

                targetItem = enumerator.Current;

            }

            int targetIndex = Owner.ListSource.IndexOf(targetItem);
            if (this.DraggedItems != null)
            {

                foreach (ListViewDataItem item in this.DraggedItems)
                {
                    int currentIndex = Owner.ListSource.IndexOf(item);
                    Owner.ListSource.Move(currentIndex, targetIndex);
                    if (currentIndex > targetIndex)
                    {
                        targetIndex++;
                    }
                }
            }
            else if (this.DraggedItem != null)
            {
                int currentIndex = Owner.ListSource.IndexOf(this.DraggedItem);
                if (dropAtEnd)
                {
                    currentIndex = Owner.ListSource.Count - 1;
                }
                else if (currentIndex < targetIndex)
                {
                    targetIndex--;
                }

                Owner.ListSource.Move(Owner.ListSource.IndexOf(this.DraggedItem), targetIndex);
            }

            targetItem.Owner.ListSource.EndUpdate();
            targetItem.Owner.ViewElement.Scroller.UpdateScrollValue();
        }
        else if (targetViewElement != null)
        {
            targetViewElement.Owner.ListSource.BeginUpdate();
            if (this.DraggedItems != null)
            {
                foreach (ListViewDataItem item in this.DraggedItems)
                {
                    targetViewElement.Owner.ListSource.Move(Owner.ListSource.IndexOf(item), Owner.ListSource.Count - 1);
                }
            }
            else if (this.DraggedItem != null)
            {
                targetViewElement.Owner.ListSource.Move(Owner.ListSource.IndexOf(this.DraggedItem), Owner.ListSource.Count - 1);
            }

            targetViewElement.Owner.ListSource.EndUpdate();
            targetViewElement.Owner.ViewElement.Scroller.UpdateScrollValue();
        }
    }
}

Regards,
Dinko | Tech Support Engineer
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.