KB Article A1445
Visible to All Users

Drag-and-drop of multiple selected grid rows

Description:
I'm implementing the drag&drop functionality in my XtraGrid. The grid enables multiple rows to be selected (the MultiSelect option of OptionsSelection is activated). Is there any peculiarity in this scenario I must be aware of?

Answer:
There are two aspects specific to implementing multi-row drag&drop:

  1. The grid uses the indicator column to select rows via the mouse in multi-select mode. Therefore, drag&drop should not start when the indicator column is clicked to avoid a conflict with rows selection feature of the grid.
  2. By default, the grid activates an in-place editor when the left mouse button is pressed. If your grid is editable, you should set the EditorShowMode option of GridView.OptionsBehavior to MouseUp or Click, so that the in-place editor is not opened when drag&drop is initialized.
    Below is the sample code for the MouseDown and MouseMove events. Please note that the grid's HitTest is checked to make sure it's not equal to the GridHitTest.RowIndicator to satisfy condition #1 listed above.
C#
using DevExpress.XtraGrid.Views.Grid; using DevExpress.XtraGrid.Views.Grid.ViewInfo; GridHitInfo downHitInfo = null; private void gridView1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e) { GridView view = sender as GridView; downHitInfo = null; GridHitInfo hitInfo = view.CalcHitInfo(new Point(e.X, e.Y)); if(Control.ModifierKeys != Keys.None) return; if(e.Button == MouseButtons.Left && hitInfo.InRow && hitInfo.HitTest != GridHitTest.RowIndicator) downHitInfo = hitInfo; } private void gridView1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e) { GridView view = sender as GridView; if(e.Button == MouseButtons.Left && downHitInfo != null) { Size dragSize = SystemInformation.DragSize; Rectangle dragRect = new Rectangle(new Point(downHitInfo.HitPoint.X - dragSize.Width / 2, downHitInfo.HitPoint.Y - dragSize.Height / 2), dragSize); if(!dragRect.Contains(new Point(e.X, e.Y))) { view.GridControl.DoDragDrop(GetDragData(view), DragDropEffects.All); downHitInfo = null; } } }
Visual Basic
Imports DevExpress.XtraGrid.Views.Grid Imports DevExpress.XtraGrid.Views.Grid.ViewInfo Private downHitInfo As GridHitInfo Private Sub GridView1_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs) Handles GridView1.MouseDown Dim view As GridView = CType(sender, GridView) downHitInfo = Nothing Dim hitInfo As GridHitInfo = view.CalcHitInfo(New Point(e.X, e.Y)) If Control.ModifierKeys <> Keys.None Then Exit Sub If e.Button = MouseButtons.Left AndAlso hitInfo.InRow AndAlso hitInfo.HitTest <> GridHitTest.RowIndicator Then downHitInfo = hitInfo End If End Sub Private Sub GridView1_MouseMove(ByVal sender As Object, ByVal e As MouseEventArgs) Handles GridView1.MouseMove Dim view As GridView = CType(sender, GridView) If e.Button = MouseButtons.Left And Not downHitInfo Is Nothing Then Dim dragSize As Size = SystemInformation.DragSize Dim dragRect As Rectangle = New Rectangle(New Point(downHitInfo.HitPoint.X - dragSize.Width / 2, _ downHitInfo.HitPoint.Y - dragSize.Height / 2), dragSize) If Not dragRect.Contains(New Point(e.X, e.Y)) Then view.GridControl.DoDragDrop(GetDragData(view), DragDropEffects.All) downHitInfo = Nothing End If End If End Sub

See Also:
How to implement drag-and-drop for grid rows
Drag-and-drop data rows from one grid to another
Control.DoDragDrop Method

Show previous comments (3)
DevExpress Support Team 10 years ago

    Hello,

    See a code snippet from the Drag-and-drop of multiple selected grid rows example with the GetDragData method.
    You can find all attached examples on the right side of the current example. Look at the attached screenshot.

    Do not hesitate to contact us if you have any further questions.

      Hi,
      I copied your code into my project and It worked so good in multiple selected rows. But i can't do that with single selected row. So, please let me know how to use this code for multiple selected rows and single selected row.
      Thanks for your help.

      Andrew Ser (DevExpress Support) 9 years ago

        Hello,
        Please see the How to implement drag-and-drop for grid rows KB article describing this task. You can see how it works in the How to reorder grid rows by drag and drop example.

        See also: Drag-and-drop data rows from one grid to another

        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.