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

SfDataGrid Row drag to reorder when using Grouping

I have a SfDataGrid, and am using the row drag to let the user reorder steps; the item source is  List<StepDTO>, and mu code is like this:

private void SfGrid_QueryRowDragging(object sender, QueryRowDraggingEventArgs e)
{
 if (e.Reason == QueryRowDraggingReason.DragEnded)
 {
  int startIdx = e.From - 1;
  int endIdx = e.To - 1;
  StepDTO s = m_plan.StepList[startIdx];
  m_plan.StepList.RemoveAt(startIdx);
  m_plan.StepList.Insert(endIdx, s);
 }
}

This works great, and I like this control! But now I want to use Grouping, and got it in place and working fine. But, when the RowDragging event is called, the indexes are NOT my item indexes but rather the grid indexes which include the group header rows. So ... crash me.

I am hacling something up that might work, but would rather find out I am missing an easy trick!
Any ideas?

Thanks, Mike



3 Replies

PS Pavithra  Sivakumar Syncfusion Team June 1, 2017 06:04 PM UTC

Hi Michael, 
 
Thanks for contacting Syncfusion support. 
 
We have checked your query. We have prepared a sample based on it which can be downloaded from the below link. 
 
 
Regards, 
Pavithra S 



ME Michael Eick June 2, 2017 01:29 AM UTC

Thanks for the quick response. A couple of questions:1. Is it true that this code only allows drag within a group?2. What is the Task.Delay(3000) for? 3 seconds is a long time ...EDIT: Implemented the sample, but the sfGrid.CurrentItem is always null. Sigh.Regards, Mike


PS Pavithra  Sivakumar Syncfusion Team June 3, 2017 08:27 PM UTC

 
Hi Michael, 
 
1.     Regarding your first query of “Is it true that this code only allows drag within a group?” 
 
Yes, it allows drag only within a group. If you want drag among all groups, please refer the below code example. 
 
private void DataGrid_QueryRowDragging(object sender, QueryRowDraggingEventArgs e) 
{ 
       if (e.Reason == QueryRowDraggingReason.DragEnded) 
       { 
             if (dataGrid.GroupColumnDescriptions.Count == 0) 
             { 
 
                    int startIndex = e.From - 1; 
                    int endIndex = e.To - 1; 
                    OrderInfo orderInfo = viewModel.OrdersInfo[startIndex]; 
                    viewModel.OrdersInfo.RemoveAt(startIndex); 
                    viewModel.OrdersInfo.Insert(endIndex, orderInfo); 
             } 
             else 
            { 
                     var currentOrderInfo = e.CurrentRowData as OrderInfo; 
                     var draggedOrderInfo = e.RowData as OrderInfo; 
                     int currentOrderInfoCollectionIndex = this.viewModel.OrdersInfo.IndexOf(currentOrderInfo); 
                     int draggedOrderInfoCollectionIndex = this.viewModel.OrdersInfo.IndexOf(draggedOrderInfo); 
                     viewModel.OrdersInfo.Remove(draggedOrderInfo); 
                     if (e.From > e.To) 
                         viewModel.OrdersInfo.Insert(currentOrderInfoCollectionIndex, draggedOrderInfo); 
                     else 
                         viewModel.OrdersInfo.Insert(currentOrderInfoCollectionIndex + 1, draggedOrderInfo);                          
              } 
      } 
} 
 
2.     Regarding your second query of “ What is the Task.Delay(3000) for? 3 seconds is a long time ...”  
 
Task.Delay is to improve the performance. You can reduce the delay time for your convenience. 
 
3.     Regarding your third query of “Implemented the sample, but the sfGrid.CurrentItem is always null.” 
 
Sorry for the mistake. This condition is not needed. 
 
Regards, 
Pavithra S 
 


Loader.
Live Chat Icon For mobile
Up arrow icon