diff --git a/DragAndDropBetweenDataGrids.png b/DragAndDropBetweenDataGrids.png new file mode 100644 index 0000000..b919c42 Binary files /dev/null and b/DragAndDropBetweenDataGrids.png differ diff --git a/README.md b/README.md index ba20e99..97027bb 100644 --- a/README.md +++ b/README.md @@ -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; + +/// +/// customize the DragStart event.Restrict the certain record from dragging. +/// +/// +/// +private void sfGrid_DragStart(object sender, GridRowDragStartEventArgs e) +{ + var record = e.DraggingRecords[0] as OrderInfo; + if (record.CustomerName == "Martin") + { + e.Handled = true; + } +} + +/// +/// Customize the DragOver event.Disable the DragUI +/// +/// +/// +private void grid_DragOver(object sender, GridRowDragOverEventArgs e) +{ + e.ShowDragUI = false; + e.Handled = true; +} + + +/// +/// Customize the Drop event +/// +/// +/// +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; + } +} + +/// +/// Customize the Dropped event. +/// +/// +/// +private void sfGrid_Dropped(object sender, GridRowDroppedEventArgs e) +{ + ObservableCollection draggingRecords = new ObservableCollection(); + + draggingRecords = e.Data.GetData("Records") as ObservableCollection; + + 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.