Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added DragAndDropBetweenDataGrids.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
96 changes: 94 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,94 @@
# How to drag and drop rows between two wpf datagrids?
This example illustrates how to drag and drop rows between two wpf datagrids
# How to Drag and Drop Rows Between Two WPF DataGrids?

This example illustrates how to drag and drop rows between two [WPF DataGrid](https://www.syncfusion.com/wpf-controls/treegrid) and two [UWP DataGrid](https://www.syncfusion.com/uwp-ui-controls/datagrid) (SfDataGrid).

## WPF

To perform the dragging operation between two DataGrid by using the [GridRowDragDropController.DragStart](https://help.syncfusion.com/cr/wpf/Syncfusion.UI.Xaml.Grid.GridRowDragDropController.html#Syncfusion_UI_Xaml_Grid_GridRowDragDropController_DragStart), [GridRowDragDropController.Drop](https://help.syncfusion.com/cr/wpf/Syncfusion.UI.Xaml.Grid.GridRowDragDropController.html#Syncfusion_UI_Xaml_Grid_GridRowDragDropController_Drop), [GridRowDragDropController.DragOver](https://help.syncfusion.com/cr/wpf/Syncfusion.UI.Xaml.Grid.GridRowDragDropController.html#Syncfusion_UI_Xaml_Grid_GridRowDragDropController_DragOver) and [GridRowDragDropController.Dropped](https://help.syncfusion.com/cr/wpf/Syncfusion.UI.Xaml.Grid.GridRowDragDropController.html#Syncfusion_UI_Xaml_Grid_GridRowDragDropController_Dropped) events.

``` c#
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;
}
```

![Drag and drop between DataGrids](DragAndDropBetweenDataGrids.png)

## UWP

You should enable [AllowDraggingRows](https://help.syncfusion.com/cr/uwp/Syncfusion.UI.Xaml.Grid.SfDataGrid.html#Syncfusion_UI_Xaml_Grid_SfDataGrid_AllowDraggingRows) and [AllowDrop](https://learn.microsoft.com/en-us/uwp/api/windows.ui.xaml.uielement.allowdrop?view=winrt-22621) property for the DataGrid which are involved in row drag and drop operations.