Example E752
Visible to All Users

WinForms Data Grid - Drag-and-drop multiple grid rows

This example shows how to use the Drag And Drop Behavior (available in v17.2+) to allow a user to move selected rows from the GridControl to the ListBoxControl using drag-and-drop. You should also handle the following drag-drop-related events:

C#
DragDropBehavior listBoxBehavior = behaviorManager.GetBehavior<DragDropBehavior>(this.listBoxControl); listBoxBehavior.DragOver += ListBoxBehavior_DragOver; listBoxBehavior.DragDrop += ListBoxBehavior_DragDrop;

Note

In older versions, use the standard Drag events. Read the following topic for more information: Drag-and-drop of multiple selected grid rows.

Files to Review

Documentation

Does this example address your development requirements/objectives?

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

Example Code

Form1.cs(vb)
C#
using DevExpress.Utils.DragDrop; using DevExpress.XtraGrid; using DevExpress.XtraGrid.Views.Grid; using System.Data; using System.Windows.Forms; namespace DragDropExample { 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("ItemName"); for (int i = 1; i <= 10; i++) table.Rows.Add(new object[] { i, "Item " + i.ToString() }); return table; } public void SetUpGrid(GridControl grid, DataTable table) { GridView view = grid.MainView as GridView; grid.DataSource = table; view.OptionsBehavior.Editable = false; view.OptionsSelection.MultiSelect = true; view.OptionsSelection.MultiSelectMode = GridMultiSelectMode.RowSelect; view.SelectRange(3, 7); } public void HandleBehaviorDragDropEvents() { DragDropBehavior listBoxBehavior = behaviorManager1.GetBehavior<DragDropBehavior>(this.listBoxControl1); listBoxBehavior.DragOver += ListBoxBehavior_DragOver; listBoxBehavior.DragDrop += ListBoxBehavior_DragDrop; } private void ListBoxBehavior_DragDrop(object sender, DragDropEventArgs e) { int[] rowHandles = (int[])e.Data; object[] rows = GetRows(this.gridView1, rowHandles); listBoxControl1.Items.Clear(); foreach (object row in rows) { string name = (row as DataRowView)["ItemName"].ToString(); listBoxControl1.Items.Add(name); } } private void ListBoxBehavior_DragOver(object sender, DragOverEventArgs e) { e.Default(); Cursor.Current = Cursors.Default; } private object[] GetRows(GridView view, int[] rowHandles) { if (rowHandles.Length == 0) return null; object[] rows = new object[rowHandles.Length]; for (int i = 0; i < rowHandles.Length; i++) { DataRowView row = view.GetRow(rowHandles[i]) as DataRowView; rows[i] = row; } return rows; } } }

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.