简体   繁体   English

在C#中拖放排序 - 最佳算法

[英]Drag drop sorting in C# - Best algorithm

I have a Telerik RadGrid which allows the user to drag rows to reorder them. 我有一个Telerik RadGrid,它允许用户拖动行来重新排序它们。 What will happen on the server side is an int column for each row will be modified based on where they've dragged, then we sort on this figure on the screens. 服务器端会发生什么,每行的int列将根据它们拖动的位置进行修改,然后我们在屏幕上对此图进行排序。

What I can't figure out is the easiest way to handle the reordering in the code behind. 我无法弄清楚的是处理后面代码中重新排序的最简单方法。 What's the easiest/best way of updating incrementing/decrementing any rows 'inbetween' the dragged rows? 更新递增/递减拖动行之间的任何行的最简单/最好的方法是什么?

EDIT: What I have so far 编辑:到目前为止我所拥有的

     //We should only allow one section to be dragged at a time
            GridDataItem draggedItem = e.DraggedItems.Single();
            TakeoffSection draggedSection = dc.TakeoffSections.Where(a => a.ID == (long)draggedItem.GetDataKeyValue("ID")).Single(); 
            int origDragSectNo = draggedSection.SectionNo;

            GridDataItem destItem = e.DestDataItem;
            TakeoffSection destSection = dc.TakeoffSections.Where(a => a.ID == (long)destItem.GetDataKeyValue("ID")).Single(); 
            int origDestSectNo = destSection.SectionNo;

            if (e.DropPosition == GridItemDropPosition.Above)
            {
                if (draggedSection.SectionNo < destSection.SectionNo)
                {
                    //They are dragging *down*!
                    draggedSection.SectionNo = destSection.SectionNo;
                    destSection.SectionNo++;

                    foreach (var item in dc.TakeoffSections.Where(a => a.RevisionID == ActiveRevisionID && a.SectionNo < origDestSectNo && a.SectionNo > origDestSectNo))
                        item.SectionNo--;
                    dc.SubmitChanges();
                }
                else
                {
                    //They are dragging *up*

                }
            }

Basically, you need to update all items' SectionNo. 基本上,您需要更新所有项目的SectionNo。 In other words, you are not swapping two items instead you are pushing down SectionNo of items below. 换句话说,您没有交换两个项目而是按下下面的项目的SectionNo。

Here is the algorithm I use. 这是我使用的算法。

protected void RadGrid1_RowDrop(object sender, GridDragDropEventArgs e)
{
   var ids = (from GridDataItem item in this.RadGrid1.Items
            select Convert.ToInt32(item.GetDataKeyValue("ID"))).ToList();

   // Rearranges item in requested order
   if (ids.Count > 0 && e.DestDataItem != null)
   {
     // Get the index of destination row
     int destItem = ids.FirstOrDefault(item => 
       item == Convert.ToInt32(e.DestDataItem.GetDataKeyValue("ID")));

     int destinationIndex = destItem == 0 ? -1 : ids.IndexOf(destItem);

     foreach (GridDataItem draggedItem in e.DraggedItems)
     {
       int draggedId = ids.FirstOrDefault(item => 
         item == Convert.ToInt32(draggedItem.GetDataKeyValue("ID")));

       if (draggedId != 0 && destinationIndex > -1)
       {
         // Remove and re-insert at specified index
         ids.Remove(draggedId);
         ids.Insert(destinationIndex, draggedId);
       }
     }
   }

   // Update each entity's display orders based on the given order
   MyUpdateDisplayOrder(ids);

   RadGrid1.Rebind();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM