下面的示例假设在网格控件中有一个主/从关系。 主表视图由 网格视图 呈现,细节视图由 卡片视图 类的实例呈现。 本示例展示了如何在细节视图间实现卡片拖动。

Control.MouseDown 事件用于识别被单击的卡片,并初始拖动操作。 然后 Control.DragOver 事件用于确定是否允许放落。 GridControl.GetViewAt 方法用于获取当前位于鼠标指针下面的视图。 如果获取的视图是一个细节视图 (卡片视图),则允许放落。

最后,Control.DragDrop 事件用于确定卡片已放落的视图 (再次使用 GridControl.GetViewAt 方法)。 接下来,通过修改指向主表记录的字段值,把被拖动的卡片移动到放落的视图。

注意,要允许放落操作,则应该把网格控件的 AllowDrop 属性值设置为 true

C#CopyCode image复制代码
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 = gridControl1.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 = gridControl1.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;
}

Visual BasicCopyCode image复制代码
Imports DevExpress.XtraGrid.Views.Base
Imports DevExpress.XtraGrid.Views.Card
Imports DevExpress.XtraGrid.Views.Grid
Imports DevExpress.XtraGrid
Imports DevExpress.XtraGrid.Views.Card.ViewInfo
// ...
Private Sub Form1_MouseDown(ByVal sender As Object, _
ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseDown
   Dim View As CardView = sender
   Dim ParentView As GridView = View.ParentView
   ' Obtain the clicked point.
   Dim Hi As CardHitInfo = View.CalcHitInfo(New Point(e.X, e.Y))
   ' Determine if the clicked point belongs to a card caption.
   If Hi.HitTest = CardHitTest.CardCaption Then
      ' Obtain the clicked card.
      Dim RowID As String = View.GetRowCellValue(Hi.RowHandle, _
        View.Columns("ProductID")).ToString()
      GridControl1.DoDragDrop(RowID, DragDropEffects.Move)
   End If
End Sub
// ...
Private Sub Form1_DragOver(ByVal sender As Object, _
ByVal e As System.Windows.Forms.DragEventArgs) Handles MyBase.DragOver
   Dim Grid As GridControl = sender
   ' Obtain the view over which the card is being dragged.
   Dim pt As Point = GridControl1.PointToClient(New Point(e.X, e.Y));
   Dim View As BaseView = Grid.GetViewAt(pt)
   If TypeOf View Is CardView Then
      e.Effect = DragDropEffects.Move
   Else
      e.Effect = DragDropEffects.None
   End If
End Sub
// ...
Private Sub Form1_DragDrop(ByVal sender As Object, _
ByVal e As System.Windows.Forms.DragEventArgs) Handles MyBase.DragDrop
   Dim Grid As GridControl = sender
   ' Obtain the view to which the card has been dropped.
   Dim pt As Point = GridControl1.PointToClient(New Point(e.X, e.Y));
   Dim View As CardView = Grid.GetViewAt(pt)
   Dim dragData As String = e.Data.GetData(DataFormats.Text).ToString()
   Dim RowID As Integer = Convert.ToInt32(dragData)
   ' Obtain the row owning the detail clone to which the card is dropped.
   Dim parentView As GridView = View.ParentView
   Dim cellValue As Object = parentView.GetRowCellValue(view.SourceRowHandle, _
     parentView.Columns("CategoryID"))
   Dim currentParentID As Integer = Convert.ToInt32(cellValue)
   ' Change the parent row of the card results in moving this card to specified parent row.
   DataSet11.Products.Rows.Find(RowID)(DataSet11.Products.Columns("CategoryID")) = _
     currentParentID
End Sub