Skip to content

Commit 5712354

Browse files
authored
Merge pull request #4 from SyncfusionExamples/ES-975464
ES-975464 - Resolve the ReadMe file length issue in this sample repository
2 parents f814866 + 454359e commit 5712354

File tree

2 files changed

+94
-2
lines changed

2 files changed

+94
-2
lines changed

DragAndDropBetweenDataGrids.png

87.3 KB
Loading

README.md

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

0 commit comments

Comments
 (0)