|
1 | | -# How to drag and drop rows between datagrid and listview in wpf? |
2 | | -This example illustrates how to drag and drop rows between datagrid and listview in wpf |
| 1 | +# How to Drag and Drop Rows Between WPF DataGrid and WPF TreeGrid? |
| 2 | + |
| 3 | +This example illustrates how to drag and drop rows between [WPF DataGrid](https://www.syncfusion.com/wpf-controls/datagrid) (SfDataGrid) and [WPF TreeGrid](https://www.syncfusion.com/wpf-controls/treegrid) (SfTreeGrid). |
| 4 | + |
| 5 | +To perform the dragging operation between DataGrid and TreeGrid by using the [GridRowDragDropController.Drop](https://help.syncfusion.com/cr/wpf/Syncfusion.UI.Xaml.Grid.GridRowDragDropController.html#Syncfusion_UI_Xaml_Grid_GridRowDragDropController_Drop) and [TreeGridRowDragDropController.Drop](https://help.syncfusion.com/cr/wpf/Syncfusion.UI.Xaml.TreeGrid.TreeGridRowDragDropController.html#Syncfusion_UI_Xaml_TreeGrid_TreeGridRowDragDropController_Drop) events. |
| 6 | + |
| 7 | +``` c# |
| 8 | +this.sfDataGrid.RowDragDropController.Drop += sfDataGrid_Drop; |
| 9 | +this.sfTreeGrid.RowDragDropController.Drop += sfTreeGrid_Drop; |
| 10 | + |
| 11 | +/// <summary> |
| 12 | +/// Customized TreeGrid Drop event. |
| 13 | +/// </summary> |
| 14 | +/// <param name="sender"></param> |
| 15 | +/// <param name="e"></param> |
| 16 | +private void sfTreeGrid_Drop(object sender, TreeGridRowDropEventArgs e) |
| 17 | +{ |
| 18 | + if (e.IsFromOutSideSource) |
| 19 | + { |
| 20 | + var draggingRecord = e.Data.GetData("Records") as ObservableCollection<object>; |
| 21 | + var record = draggingRecord[0] as EmployeeInfo; |
| 22 | + var dropPosition = e.DropPosition.ToString(); |
| 23 | + var newItem = new EmployeeInfo(); |
| 24 | + var rowIndex =AssociatedObject.sfTreeGrid.ResolveToRowIndex(e.TargetNode.Item); |
| 25 | + if (dropPosition != "None" && rowIndex != -1) |
| 26 | + { |
| 27 | + if (AssociatedObject.sfTreeGrid.View is TreeGridSelfRelationalView) |
| 28 | + { |
| 29 | + var treeNode = e.TargetNode; |
| 30 | + if (treeNode == null) |
| 31 | + return; |
| 32 | + var data = treeNode.Item; |
| 33 | + AssociatedObject.sfTreeGrid.SelectionController.SuspendUpdates(); |
| 34 | + var dropIndex = -1; |
| 35 | + TreeNode parentNode = null; |
| 36 | + if (dropPosition == "DropBelow" || dropPosition == "DropAbove") |
| 37 | + { |
| 38 | + parentNode = treeNode.ParentNode; |
| 39 | + if (parentNode == null) |
| 40 | + { |
| 41 | + var treeNodeItem = treeNode.Item as EmployeeInfo; |
| 42 | + newItem = new EmployeeInfo() { FirstName = record.FirstName, LastName = record.LastName, ID = record.ID, Salary = record.Salary, Title = record.Title, ReportsTo = treeNodeItem.ReportsTo }; |
| 43 | + } |
| 44 | + else |
| 45 | + { |
| 46 | + var parentNodeItems = parentNode.Item as EmployeeInfo; |
| 47 | + newItem = new EmployeeInfo() { FirstName = record.FirstName, LastName = record.LastName, ID = record.ID, Salary = record.Salary, Title = record.Title, ReportsTo = parentNodeItems.ID }; |
| 48 | + } |
| 49 | + } |
| 50 | + else if (dropPosition == "DropAsChild") |
| 51 | + { |
| 52 | + if (!treeNode.IsExpanded) |
| 53 | + AssociatedObject.sfTreeGrid.ExpandNode(treeNode); |
| 54 | + parentNode = treeNode; |
| 55 | + var parentNodeItems = parentNode.Item as EmployeeInfo; |
| 56 | + newItem = new EmployeeInfo() { FirstName = record.FirstName, LastName = record.LastName, ID = record.ID, Salary = record.Salary, Title = record.Title, ReportsTo = parentNodeItems.ID }; |
| 57 | + } |
| 58 | + IList sourceCollection = null; |
| 59 | + if (dropPosition == "DropBelow" || dropPosition == "DropAbove") |
| 60 | + { |
| 61 | + if (treeNode.ParentNode != null) |
| 62 | + { |
| 63 | + var collection = AssociatedObject.sfTreeGrid.View.GetPropertyAccessProvider().GetValue(treeNode.ParentNode.Item, AssociatedObject.sfTreeGrid.ChildPropertyName) as IEnumerable; |
| 64 | + sourceCollection = GetSourceListCollection(collection); |
| 65 | + } |
| 66 | + else |
| 67 | + { |
| 68 | + sourceCollection = GetSourceListCollection(AssociatedObject.sfTreeGrid.View.SourceCollection); |
| 69 | + } |
| 70 | + dropIndex = sourceCollection.IndexOf(data); |
| 71 | + if (dropPosition == "DropBelow") |
| 72 | + { |
| 73 | + dropIndex += 1; |
| 74 | + } |
| 75 | + } |
| 76 | + else if (dropPosition == "DropAsChild") |
| 77 | + { |
| 78 | + var collection = AssociatedObject.sfTreeGrid.View.GetPropertyAccessProvider().GetValue(data, AssociatedObject.sfTreeGrid.ChildPropertyName) as IEnumerable; |
| 79 | + sourceCollection = GetSourceListCollection(collection); |
| 80 | + if (sourceCollection == null) |
| 81 | + { |
| 82 | + var list = data.GetType().GetProperty(AssociatedObject.sfTreeGrid.ChildPropertyName).PropertyType.CreateNew() as IList; |
| 83 | + if (list != null) |
| 84 | + { |
| 85 | + AssociatedObject.sfTreeGrid.View.GetPropertyAccessProvider().SetValue(treeNode.Item, AssociatedObject.sfTreeGrid.ChildPropertyName, list); |
| 86 | + sourceCollection = list; |
| 87 | + } |
| 88 | + } |
| 89 | + dropIndex = sourceCollection.Count; |
| 90 | + } |
| 91 | + sourceCollection.Insert(dropIndex, newItem); |
| 92 | + AssociatedObject.sfTreeGrid.SelectionController.ResumeUpdates(); |
| 93 | + (AssociatedObject.sfTreeGrid.SelectionController as TreeGridRowSelectionController).RefreshSelection(); |
| 94 | + e.Handled = true; |
| 95 | + } |
| 96 | + } |
| 97 | + AssociatedObject.sfDataGrid.View.Remove(record); |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +/// <summary> |
| 102 | +/// Gets the source collection of TreeGrid |
| 103 | +/// </summary> |
| 104 | +/// <param name="collection"></param> |
| 105 | +/// <returns></returns> |
| 106 | +private IList GetSourceListCollection(IEnumerable collection) |
| 107 | +{ |
| 108 | + IList list = null; |
| 109 | + if (collection == null) |
| 110 | + collection = AssociatedObject.sfTreeGrid.View.SourceCollection; |
| 111 | + if ((collection as IList) != null) |
| 112 | + { |
| 113 | + list = collection as IList; |
| 114 | + } |
| 115 | + return list; |
| 116 | +} |
| 117 | + |
| 118 | +/// <summary> |
| 119 | +/// Customize the Drop event.restrict the certain record and Drop position from drop. |
| 120 | +/// </summary> |
| 121 | +/// <param name="sender"></param> |
| 122 | +/// <param name="e"></param> |
| 123 | +private void sfDataGrid_Drop(object sender, GridRowDropEventArgs e) |
| 124 | +{ |
| 125 | + if (e.IsFromOutSideSource) |
| 126 | + { |
| 127 | + var draggingRecord = e.Data.GetData("Nodes") as ObservableCollection<TreeNode>; |
| 128 | + var record = draggingRecord[0].Item as EmployeeInfo; |
| 129 | + int dropIndex = (int)e.TargetRecord; |
| 130 | + var dropPosition = e.DropPosition.ToString(); |
| 131 | + if (record.Title == "Manager") |
| 132 | + { |
| 133 | + e.Handled = true; |
| 134 | + return; |
| 135 | + } |
| 136 | + IList collection = null; |
| 137 | + collection = AssociatedObject.sfDataGrid.View.SourceCollection as IList; |
| 138 | + if (dropPosition == "DropAbove") |
| 139 | + { |
| 140 | + dropIndex--; |
| 141 | + collection.Insert(dropIndex, record); |
| 142 | + } |
| 143 | + else |
| 144 | + { |
| 145 | + dropIndex++; |
| 146 | + collection.Insert(dropIndex, record); |
| 147 | + } |
| 148 | + AssociatedObject.sfTreeGrid.View.Remove(record); |
| 149 | + e.Handled = true; |
| 150 | + } |
| 151 | +} |
| 152 | +``` |
| 153 | + |
| 154 | + |
0 commit comments