2012-11-20 3 views
1

사용자가 행을 드래그하여 순서를 변경할 수있는 Telerik RadGrid가 있습니다. 서버 측에서 일어날 일은 각 행에 대한 int 열은 그들이 드래그 한 위치를 기반으로 수정되고 화면에서이 수치를 정렬합니다.C#의 드래그 드롭 정렬 - 최상의 알고리즘

내가 알 수없는 것은 코드 뒤에서 순서 재 지정을 처리하는 가장 쉬운 방법입니다. 드래그 된 행의 'inbetween'행을 증가/감소시키는 가장 쉽고/가장 좋은 방법은 무엇입니까?

편집 : 나는 지금까지

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

       } 
      } 
+0

질문이 명확하지 않습니다. 현재 구현에서 어떤 문제가 있습니까? 쓰여진 방식을 단순히 싫어합니까? 너무 느린가요? 너무 느린 경우 병목 현상은 어디에서 발생합니까? – Groo

답변

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(); 
} 
+0

감사합니다. @Win! 날 구했어. – darkfreq

관련 문제