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:
- 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.
- 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 BasicImports 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
Hi,
I'm using this exact Approach to enable drag&drop in my grids. However, because the EditorShowMode is now MouseUp, the first click into a column with a Checkbox Editor is doing nothing. Our customers not only want drag&drop, they also want to toggle a Checkbox with one click only.
Is there a solution to both enable drag&drop like shown above AND to allow the users to change toggles with one click only?
Thanks,
Walter
A similar issue is described in the How to toggle a checkbox editor with a single click when EditorShowModue = MouseUp topic. Please review it. I hope you will find this information useful. If you need any further assistance with this subject, please feel free to reactivate this ticket.
Hi,
What is GetDragData() function used in DoDragDrop method?
Regards,
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.
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