|
1 | | -# How to drag and drop rows between two wpf datagrids? |
2 | | -This example illustrates how to drag and drop rows between two wpf datagrids |
| 1 | +# How to drag and drop rows between two wpf datagrids |
| 2 | + |
| 3 | +This example illustrates how to drag and drop rows between two [WPF DataGrid](https://www.syncfusion.com/wpf-controls/treegrid). |
| 4 | + |
| 5 | +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. |
| 6 | + |
| 7 | +``` c# |
| 8 | +this.firstDataGrid.RowDragDropController.DragStart += sfGrid_DragStart; |
| 9 | +this.firstDataGrid.RowDragDropController.Drop += sfGrid_Drop; |
| 10 | +this.firstDataGrid.RowDragDropController.Dropped += sfGrid_Dropped; |
| 11 | +this.secondDataGrid.RowDragDropController.DragOver += grid_DragOver; |
| 12 | + |
| 13 | +/// <summary> |
| 14 | +/// customize the DragStart event.Restrict the certain record from dragging. |
| 15 | +/// </summary> |
| 16 | +/// <param name="sender"></param> |
| 17 | +/// <param name="e"></param> |
| 18 | +private void sfGrid_DragStart(object sender, GridRowDragStartEventArgs e) |
| 19 | +{ |
| 20 | + var record = e.DraggingRecords[0] as OrderInfo; |
| 21 | + if (record.CustomerName == "Martin") |
| 22 | + { |
| 23 | + e.Handled = true; |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +/// <summary> |
| 28 | +/// Customize the DragOver event.Disable the DragUI |
| 29 | +/// </summary> |
| 30 | +/// <param name="sender"></param> |
| 31 | +/// <param name="e"></param> |
| 32 | +private void grid_DragOver(object sender, GridRowDragOverEventArgs e) |
| 33 | +{ |
| 34 | + e.ShowDragUI = false; |
| 35 | + e.Handled = true; |
| 36 | +} |
| 37 | + |
| 38 | + |
| 39 | +/// <summary> |
| 40 | +/// Customize the Drop event |
| 41 | +/// </summary> |
| 42 | +/// <param name="sender"></param> |
| 43 | +/// <param name="e"></param> |
| 44 | +private void sfGrid_Drop(object sender,GridRowDropEventArgs e) |
| 45 | +{ |
| 46 | + var record = e.DraggingRecords[0] as OrderInfo; |
| 47 | + var dropPosition = e.DropPosition.ToString(); |
| 48 | + if (dropPosition == "DropAbove") |
| 49 | + { |
| 50 | + e.Handled = true; |
| 51 | + } |
| 52 | + if (record.ShipCity == "Mexico D.F.") |
| 53 | + { |
| 54 | + e.Handled = true; |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +/// <summary> |
| 59 | +/// Customize the Dropped event. |
| 60 | +/// </summary> |
| 61 | +/// <param name="sender"></param> |
| 62 | +/// <param name="e"></param> |
| 63 | +private void sfGrid_Dropped(object sender, GridRowDroppedEventArgs e) |
| 64 | +{ |
| 65 | + ObservableCollection<object> draggingRecords = new ObservableCollection<object>(); |
| 66 | + |
| 67 | + draggingRecords = e.Data.GetData("Records") as ObservableCollection<object>; |
| 68 | + |
| 69 | + var items = draggingRecords[0] as OrderInfo; |
| 70 | + |
| 71 | + var records = AssociatedObject.firstDataGrid.View.Records.ToList(); |
| 72 | + |
| 73 | + IList collection = AssociatedObject.firstDataGrid.ItemsSource as IList; |
| 74 | + |
| 75 | + for (int i = 0; i < records.Count; i++) |
| 76 | + { |
| 77 | + var orderData = records[i].Data as OrderInfo; |
| 78 | + if (orderData.OrderID == items.OrderID) |
| 79 | + { |
| 80 | + collection.Remove(items); |
| 81 | + collection.Insert(i, orderData); |
| 82 | + } |
| 83 | + } |
| 84 | + AssociatedObject.firstDataGrid.ItemsSource = collection; |
| 85 | +} |
| 86 | +``` |
0 commit comments