I have downloaded the following sample and then built it.
http://www.syncfusion.com/kb/4116/how-to-drag-and-drop-the-row-data-from-one-sfdatagrid-to-another
But that did not work.
===================================================================================================================================================================
using System.Collections.ObjectModel;
using Syncfusion.Data;
using Syncfusion.UI.Xaml.Grid;
using Syncfusion.UI.Xaml.ScrollAxis;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Input;
using System.Windows.Media;
using Syncfusion.UI.Xaml.Grid.Helpers;
namespace SfDataGrid_RowDragAndDrop
{
public class GridSelectionControllerExt:GridSelectionController
{
VisualContainer visualContainer;
public GridSelectionControllerExt(SfDataGrid datagrid)
: base(datagrid)
{
//Loaded event of SfDataGrid is wired here
datagrid.Loaded += datagrid_Loaded;
//Unloaded event of SfDataGrid is wired here
datagrid.Unloaded += datagrid_Unloaded;
}
void datagrid_Loaded(object sender, RoutedEventArgs e)
{
//You can get the VisualContainer ,with the help of GetVisualContainer() helper method
visualContainer = DataGrid.GetVisualContainer();
//Fired when the row data is dropped on the specified position
visualContainer.PreviewDrop += visualContainer_PreviewDrop;
//Fired when the mouse operation is performed
visualContainer.PreviewMouseMove += visualContainer_PreviewMouseMove;
}
//Initially IsPointerPressed will be false
public bool IsPointerPressed = false;
//Intially the PressedRowColumnIndex will be empty
RowColumnIndex PressedRowColumnIndex = RowColumnIndex.Empty;
protected override void ProcessPointerPressed(MouseButtonEventArgs args, RowColumnIndex rowColumnIndex)
{
//IsPointerPressed will be true if the pointer is pressed
IsPointerPressed = true;
//The currently pressed rowColumnIndex is assigned to PressedRowColumnIndex
PressedRowColumnIndex = rowColumnIndex;
base.ProcessPointerPressed(args, rowColumnIndex);
}
void visualContainer_PreviewMouseMove(object sender, MouseEventArgs e)
{
var rowColumnIndex = visualContainer.PointToCellRowColumnIndex(e.GetPosition(visualContainer));
if (!rowColumnIndex.IsEmpty && IsPointerPressed)
{
// Get the RecordIndex using RowIndex
var index = DataGrid.ResolveToRecordIndex(rowColumnIndex.RowIndex);
if (index != -1)
{
// Get the RecordIndex using RowIndex
var recordIndex = DataGrid.ResolveToRecordIndex(rowColumnIndex.RowIndex);
// Get the Record Data as RecordEntry using RecordIndex
var recordEntry = DataGrid.View.Records[recordIndex];
var data = new DataObject();
//Stores the specified DataGrid to this data, along with specified format "DataGrid"
data.SetData("DataGrid", DataGrid);
//Stores the specified recordEntry to this data, along with specified format "RecordEntry"
data.SetData("RecordEntry", recordEntry);
//Initiate the drag and drop operation for the specified source and data with permitted drag and drop effects
DragDrop.DoDragDrop(DataGrid, data, DragDropEffects.Copy | DragDropEffects.Move);
}
}
}
private void visualContainer_PreviewDrop(object sender, DragEventArgs e)
{
//Checks whether the datagrid/recordentry is available on the format
if (!e.Data.GetDataPresent("DataGrid") || !e.Data.GetDataPresent("RecordEntry"))
return;
//Get the current data grid whose row to be dragged
var dragginggrid = e.Data.GetData("DataGrid") as SfDataGrid;
//Get the record entry from the row of dragging grid
var recordent = e.Data.GetData("RecordEntry") as RecordEntry;
//Check whether the dragginggrid and record is not equal to null
if (dragginggrid == null || recordent == null)
return;
//After dropping the row the IsPointerPressed state should be false
(dragginggrid.SelectionController as GridSelectionControllerExt).IsPointerPressed = false;
// Get the current RowColumnIndex based on the VisualContainer posotion
var rowColumnIndex = visualContainer.PointToCellRowColumnIndex(e.GetPosition(visualContainer));
var draggingGrid_Source = dragginggrid.ItemsSource as ObservableCollection<Student>;
var droppingGrid_Source = DataGrid.ItemsSource as ObservableCollection<Student>;
if (!rowColumnIndex.IsEmpty)
{
// Get the RecordIndex using RowIndex
var index = DataGrid.ResolveToRecordIndex(rowColumnIndex.RowIndex);
//if the dragged row data is dropped inside the DataRow , then it will be processed here.
if (index != -1)
{
// Remove the Selected Row Data from Dragging SfDataGrid
draggingGrid_Source.Remove(recordent.Data as Student);
// Insert the Selected row data to specified index position of dropping SfDataGri
droppingGrid_Source.Insert(index, recordent.Data as Student);
// Selected Item is reset with new dropped row data
DataGrid.SelectedItem = droppingGrid_Source[index];
this.MoveCurrentCell(new RowColumnIndex(rowColumnIndex.RowIndex, 1));
}
//if the dragged row data is dropped on the HeaderRow ,then it will be processed here.
else
{
// Insert the Selected row data to specified index position of dropping SfDataGrid
droppingGrid_Source.Insert(index + 1, recordent.Data as Student);
// Remove the Selected Row Data from Dragging SfDataGrid
draggingGrid_Source.Remove(recordent.Data as Student);
// Selected Item is reset with new dropped row data
DataGrid.SelectedItem = recordent.Data;
this.MoveCurrentCell(new RowColumnIndex(rowColumnIndex.RowIndex, 1));
}
}
//if the dragged row data is dropped in empty space of SfDataGrid , it will be processed here
else
{
//IsPointerPressed will be false if the pointer is released
IsPointerPressed = false;
//Get the record count of the dropping grid ,Since the row and column index is empty
var recordcount = this.DataGrid.View.Records.Count;
// Remove the Selected Row Data from Dragging SfDataGrid
draggingGrid_Source.Remove(recordent.Data as Student);
// Insert the Selected row data to specified index position of dropping SfDataGri
droppingGrid_Source.Insert(recordcount, recordent.Data as Student);
// Selected Item is reset with new dropped row data
DataGrid.SelectedItem = droppingGrid_Source[recordcount];
//Move the selection to currently dropped row
rowColumnIndex.RowIndex = recordcount + 1;
this.MoveCurrentCell(new RowColumnIndex(rowColumnIndex.RowIndex, 1));
}
}
protected override void ProcessPointerReleased(MouseButtonEventArgs args, RowColumnIndex rowColumnIndex)
{
//IsPointerPressed will be false if the pointer is released
IsPointerPressed = false;
base.ProcessPointerReleased(args, rowColumnIndex);
}
void datagrid_Unloaded(object sender, RoutedEventArgs e)
{
//Unwire events
DataGrid.Loaded -= datagrid_Loaded;
visualContainer.PreviewDrop -= visualContainer_PreviewDrop;
visualContainer.PreviewMouseMove -= visualContainer_PreviewMouseMove;
DataGrid.Unloaded -= datagrid_Unloaded;
}
}
}