Example E3921
Visible to All Users

WPF Data Grid - Manually Control Drag-and-Drop

This example demonstrates how to use drag-and-drop events to customize the drag-and-drop behavior in the GridControl.
A drag-and-drop operation changes the Position and Department values based on the dropped record's new location.

Files to Look At

Documentation

More Examples

Does this example address your development requirements/objectives?

(you will be redirected to DevExpress.com to submit your response)

Example Code

MainWindow.xaml
XAML
<Window x:Class="WPF_GridControl_Custom_Drag_and_Drop.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:dxg="http://schemas.devexpress.com/winfx/2008/xaml/grid" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Height="350" Width="525"> <dxg:GridControl Name="gridControl" SelectionMode="Row" AutoGenerateColumns="AddNew"> <dxg:GridControl.View> <dxg:TreeListView KeyFieldName="ID" ParentFieldName="ParentID" AutoExpandAllNodes="True" AllowDragDrop="True" DropRecord="OnDropRecord"/> </dxg:GridControl.View> </dxg:GridControl> </Window>
MainWindow.xaml.cs(vb)
C#
using DevExpress.Xpf.Core; using System.Windows; namespace WPF_GridControl_Custom_Drag_and_Drop { public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); gridControl.ItemsSource = Staff.GetStaff(); } void OnDropRecord(object sender, DropRecordEventArgs e) { object data = e.Data.GetData(typeof(RecordDragDropData)); foreach (Employee employee in ((RecordDragDropData)data).Records) { employee.Position = ((Employee)e.TargetRecord).Position; employee.Department = ((Employee)e.TargetRecord).Department; } if (e.DropPosition == DropPosition.Inside) { foreach(Employee employee in ((RecordDragDropData)data).Records) { employee.Position = ""; } } } } }

Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.