This example illustrates how to drag and drop rows between two WPF DataGrid and two UWP DataGrid (SfDataGrid).
To perform the dragging operation between two DataGrid by using the GridRowDragDropController.DragStart, GridRowDragDropController.Drop, GridRowDragDropController.DragOver and GridRowDragDropController.Dropped events.
this.firstDataGrid.RowDragDropController.DragStart += sfGrid_DragStart;
this.firstDataGrid.RowDragDropController.Drop += sfGrid_Drop;
this.firstDataGrid.RowDragDropController.Dropped += sfGrid_Dropped;
this.secondDataGrid.RowDragDropController.DragOver += grid_DragOver;
/// <summary>
/// customize the DragStart event.Restrict the certain record from dragging.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void sfGrid_DragStart(object sender, GridRowDragStartEventArgs e)
{
var record = e.DraggingRecords[0] as OrderInfo;
if (record.CustomerName == "Martin")
{
e.Handled = true;
}
}
/// <summary>
/// Customize the DragOver event.Disable the DragUI
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void grid_DragOver(object sender, GridRowDragOverEventArgs e)
{
e.ShowDragUI = false;
e.Handled = true;
}
/// <summary>
/// Customize the Drop event
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void sfGrid_Drop(object sender,GridRowDropEventArgs e)
{
var record = e.DraggingRecords[0] as OrderInfo;
var dropPosition = e.DropPosition.ToString();
if (dropPosition == "DropAbove")
{
e.Handled = true;
}
if (record.ShipCity == "Mexico D.F.")
{
e.Handled = true;
}
}
/// <summary>
/// Customize the Dropped event.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void sfGrid_Dropped(object sender, GridRowDroppedEventArgs e)
{
ObservableCollection<object> draggingRecords = new ObservableCollection<object>();
draggingRecords = e.Data.GetData("Records") as ObservableCollection<object>;
var items = draggingRecords[0] as OrderInfo;
var records = AssociatedObject.firstDataGrid.View.Records.ToList();
IList collection = AssociatedObject.firstDataGrid.ItemsSource as IList;
for (int i = 0; i < records.Count; i++)
{
var orderData = records[i].Data as OrderInfo;
if (orderData.OrderID == items.OrderID)
{
collection.Remove(items);
collection.Insert(i, orderData);
}
}
AssociatedObject.firstDataGrid.ItemsSource = collection;
}You should enable AllowDraggingRows and AllowDrop property for the DataGrid which are involved in row drag and drop operations.
