How to: Implement Card Dragging Between Details
- 4 minutes to read
The following example assumes that we have a master-detail relationship within the grid control. The master View is represented by a GridView and the detail Views are represented by instances of the CardView class. This example shows how to implement card dragging between details.
The MouseDown event is used to identify the clicked card and initiate the drag operation. Then, the DragOver event is used to determine whether dropping is allowed. The GridControl.GetViewAt method is used to obtain the View currently located under the mouse pointer. If the View obtained is a detail View (a Card View), dropping is allowed.
Finally, the DragDrop event is used to determine the View where the card has been dropped (the GridControl.GetViewAt method is used again). Then, the dragged card is moved to that View by changing its field value that refers to the master record.
Note: to enable dropping you should set the grid control’s AllowDrop property value to true.
using DevExpress.XtraGrid.Views.Base;
using DevExpress.XtraGrid.Views.Card;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Views.Card.ViewInfo;
// ...
private void cvProducts_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e) {
CardView view = sender as CardView;
GridView parentView = view.ParentView as GridView;
// Obtain the clicked point.
CardHitInfo hi = view.CalcHitInfo(new Point(e.X, e.Y));
// Determine if the clicked point belongs to a card caption.
if (hi.HitTest == CardHitTest.CardCaption) {
// Obtain the clicked card.
string rowID = view.GetRowCellValue(hi.RowHandle, view.Columns["ProductID"]).ToString();
gridControl1.DoDragDrop(rowID, DragDropEffects.Move);
}
}
// ...
private void gridControl1_DragOver(object sender, System.Windows.Forms.DragEventArgs e) {
GridControl grid = sender as GridControl;
// Obtain the view over which the card is being dragged.
Point pt = grid.PointToClient(new Point(e.X, e.Y));
BaseView view = grid.GetViewAt(pt);
if (view is CardView)
e.Effect = DragDropEffects.Move;
else
e.Effect = DragDropEffects.None;
}
// ...
private void gridControl1_DragDrop(object sender, System.Windows.Forms.DragEventArgs e) {
GridControl grid = sender as GridControl;
// Obtain the view to which the card has been dropped.
Point pt = grid.PointToClient(new Point(e.X, e.Y));
CardView view = grid.GetViewAt(pt) as CardView;
string dragData = e.Data.GetData(DataFormats.Text).ToString();
int rowID = Convert.ToInt32(dragData);
// Obtain the row owning the detail clone to which the card is dropped.
GridView parentView = view.ParentView as GridView;
object cellValue = parentView.GetRowCellValue(view.SourceRowHandle,
parentView.Columns["CategoryID"]);
int currentParentID = Convert.ToInt32(cellValue);
// Change the parent row of the card results in moving this card to the specified parent row.
dataSet11.Products.Rows.Find(rowID)[dataSet11.Products.Columns["CategoryID"]] =
currentParentID;
}