2

我有一个 Telerik RadGrid,它允许用户拖动行以重新排序。在服务器端会发生的是每行的一个 int 列将根据它们拖动的位置进行修改,然后我们在屏幕上对这个数字进行排序。

我想不通的是在后面的代码中处理重新排序的最简单方法。更新在拖动行之间增加/减少任何行的最简单/最佳方法是什么?

编辑:到目前为止我所拥有的

     //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*

                }
            }
4

1 回答 1

3

基本上,您需要更新所有项目的 SectionNo。换句话说,您不是在交换两个项目,而是在下推以下项目的 SectionNo。

这是我使用的算法。

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();
}
于 2012-11-20T16:36:10.227 回答