Note:
In v23.2+, theGridView
supports the following drag-and-drop operations out of the box:
- Move selected rows to another Grid control.
- Reorder selected rows within the GridView (supported data sources: IList
, DataTable
, DataView
).
Use the GridView.OptionsDragDrop property to configure drag-and-drop operations that users can perform within the GridView.
This example demonstrates how to attach the Drag-and-Drop Behavior to the WinForms Grid Control and handle drag-drop-related events to allow users to reorder data rows using drag-and-drop.
The Behavior describes a functional aspect of a DevExpress WinForms UI control. Read the following help topic for more information: Behaviors.
Attach Drag-and-Drop Behavior to GridView
C#DragDropBehavior gridControlBehavior = behaviorManager1.GetBehavior<DragDropBehavior>(gridView); gridControlBehavior.DragDrop += Behavior_DragDrop; gridControlBehavior.DragOver += Behavior_DragOver;
Visual BasicDim gridControlBehavior As DragDropBehavior = behaviorManager1.GetBehavior(Of DragDropBehavior)(Me.gridView1)
AddHandler gridControlBehavior.DragDrop, AddressOf Behavior_DragDrop
AddHandler gridControlBehavior.DragOver, AddressOf Behavior_DragOver
Handle the DragOver Event
The DragOver event is handled to display the insert indicator and update the mouse pointer. The DragOverGridEventArgs.GetDragOverGridEventArgs
static method is used to calculate drag-and-drop settings (state). These settings are used to specify appropriate event arguments for activating the drag-and-drop functionality:
C#private void Behavior_DragOver(object sender, DragOverEventArgs e) {
DragOverGridEventArgs args = DragOverGridEventArgs.GetDragOverGridEventArgs(e);
e.InsertType = args.InsertType;
e.InsertIndicatorLocation = args.InsertIndicatorLocation;
e.Action = args.Action;
Cursor.Current = args.Cursor;
args.Handled = true;
}
Visual BasicPrivate Sub Behavior_DragOver(ByVal sender As Object, ByVal e As DragOverEventArgs)
Dim args As DragOverGridEventArgs = DragOverGridEventArgs.GetDragOverGridEventArgs(e)
e.InsertType = args.InsertType
e.InsertIndicatorLocation = args.InsertIndicatorLocation
e.Action = args.Action
Cursor.Current = args.Cursor
args.Handled = True
End Sub
Handle the DragDrop Event
The DragDrop event is handled to reorder data rows in the data source.
Files to Review
- Form1.cs (VB: Form1.vb)
- Program.cs (VB: Program.vb)
Documentation
See Also
- DevExpress WinForms Cheat Sheet - Drag-and-Drop Within/Between Controls
- DevExpress WinForms Troubleshooting - Grid Control
Does this example address your development requirements/objectives?
(you will be redirected to DevExpress.com to submit your response)
Example Code
C#using DevExpress.Utils.DragDrop;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Views.Grid.ViewInfo;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
namespace E764 {
public partial class Form1 : DevExpress.XtraEditors.XtraForm {
public Form1() {
InitializeComponent();
SetUpGrid(this.gridControl1, FillTable());
HandleBehaviorDragDropEvents();
}
public DataTable FillTable() {
DataTable table = new DataTable();
table.Columns.Add("ID", typeof(int));
table.Columns.Add("Keyword");
table.Columns.Add("Order", typeof(decimal));
table.Rows.Add(new object[] { 1, "abstract", 1 });
table.Rows.Add(new object[] { 2, "event", 2 });
table.Rows.Add(new object[] { 3, "new", 3 });
table.Rows.Add(new object[] { 4, "struct", 4 });
table.Rows.Add(new object[] { 5, "as", 5 });
table.Rows.Add(new object[] { 6, "explicit", 6 });
table.Rows.Add(new object[] { 7, "null", 7 });
table.Rows.Add(new object[] { 8, "switch", 8 });
table.Rows.Add(new object[] { 9, "base", 9 });
table.Rows.Add(new object[] { 10, "extern", 10 });
table.Rows.Add(new object[] { 11, "object", 11 });
table.Rows.Add(new object[] { 12, "this", 12 });
table.Rows.Add(new object[] { 13, "bool", 13 });
table.Rows.Add(new object[] { 14, "false", 14 });
table.Rows.Add(new object[] { 15, "operator", 15 });
table.Rows.Add(new object[] { 16, "throw", 16 });
table.Rows.Add(new object[] { 17, "break", 17 });
return table;
}
public void SetUpGrid(GridControl grid, DataTable table) {
GridView view = grid.MainView as GridView;
grid.DataSource = table;
view.OptionsBehavior.Editable = false;
}
public void HandleBehaviorDragDropEvents() {
DragDropBehavior gridControlBehavior = behaviorManager1.GetBehavior<DragDropBehavior>(this.gridView1);
gridControlBehavior.DragDrop += Behavior_DragDrop;
gridControlBehavior.DragOver += Behavior_DragOver;
}
private void Behavior_DragOver(object sender, DragOverEventArgs e) {
DragOverGridEventArgs args = DragOverGridEventArgs.GetDragOverGridEventArgs(e);
e.InsertType = args.InsertType;
e.InsertIndicatorLocation = args.InsertIndicatorLocation;
e.Action = args.Action;
Cursor.Current = args.Cursor;
args.Handled = true;
}
private void Behavior_DragDrop(object sender, DevExpress.Utils.DragDrop.DragDropEventArgs e) {
GridView targetGrid = e.Target as GridView;
GridView sourceGrid = e.Source as GridView;
if (e.Action == DragDropActions.None || targetGrid != sourceGrid)
return;
DataTable sourceTable = sourceGrid.GridControl.DataSource as DataTable;
Point hitPoint = targetGrid.GridControl.PointToClient(Cursor.Position);
GridHitInfo hitInfo = targetGrid.CalcHitInfo(hitPoint);
int[] sourceHandles = e.GetData<int[]>();
int targetRowHandle = hitInfo.RowHandle;
int targetRowIndex = targetGrid.GetDataSourceRowIndex(targetRowHandle);
List<DataRow> draggedRows = new List<DataRow>();
foreach (int sourceHandle in sourceHandles) {
int oldRowIndex = sourceGrid.GetDataSourceRowIndex(sourceHandle);
DataRow oldRow = sourceTable.Rows[oldRowIndex];
draggedRows.Add(oldRow);
}
int newRowIndex;
switch (e.InsertType) {
case InsertType.Before:
newRowIndex = targetRowIndex > sourceHandles[sourceHandles.Length - 1] ? targetRowIndex - 1 : targetRowIndex;
for (int i = draggedRows.Count - 1; i >= 0; i--) {
DataRow oldRow = draggedRows[i];
DataRow newRow = sourceTable.NewRow();
newRow.ItemArray = oldRow.ItemArray;
sourceTable.Rows.Remove(oldRow);
sourceTable.Rows.InsertAt(newRow, newRowIndex);
}
break;
case InsertType.After:
newRowIndex = targetRowIndex < sourceHandles[0] ? targetRowIndex + 1 : targetRowIndex;
for (int i = 0; i < draggedRows.Count; i++) {
DataRow oldRow = draggedRows[i];
DataRow newRow = sourceTable.NewRow();
newRow.ItemArray = oldRow.ItemArray;
sourceTable.Rows.Remove(oldRow);
sourceTable.Rows.InsertAt(newRow, newRowIndex);
}
break;
default:
newRowIndex = -1;
break;
}
int insertedIndex = targetGrid.GetRowHandle(newRowIndex);
targetGrid.FocusedRowHandle = insertedIndex;
targetGrid.SelectRow(targetGrid.FocusedRowHandle);
}
}
}