Articles in this section
Category / Section

How to enable drag and drop operation when ExcelLikeSelectionFrame property is false

2 mins read

By default, in Grid Control drag and drop operation is enabled only when ExcelLikeSelectionFrame property is true. But the user can able to perform drag and drop operations even when ExcelLikeSelectionFrame is false by overriding GridExcelLikeDragDropMouseController.

For example:  Create an ExcelDragDropController class inherited from GridExcelLikeDragDropMouseController interface and add the instance of this class into MouseControllerDispatcher of the grid.

In ExcelDragDropController class, HitTest method determines whether to perform drag and drop or not based on the mouse position. It returns 1 if the position is correct else returns 0.

 

C#:

 

public int HitTest(MouseControllerEventArgs e, IMouseController controller)
{
    Point pt = new Point(e.Location.X, e.Location.Y);
    if (e. Button == MouseButton.Left || e.ClickCount == 1)   
    {
if (controller == null || controller.Name == "SelectCellsMouseController")
{
if (CheckHitTest(grid,pt))
   return 1;
}
            return 0;
    }    
}

 

CheckHitTest method is used to determine whether the mouse is pressed in the GridControl or not.

C#

public bool CheckHitTest(GridControlBase grid, Point point)
{
  var currentRowColumnIndex = grid.PointToCellRowColumnIndex(point);
  int rowIndex = currentRowColumnIndex.RowIndex;
  int colIndex = currentRowColumnIndex.ColumnIndex;
 
  if (rowIndex <= grid.Model.RowCount && colIndex <= grid.Model.ColumnCount)
  {
      bool bSelEdge = false;
      GridRangeInfo activeRange = grid.Model.Selections.Ranges.ActiveRange;
 
      if (activeRange.IsEmpty)
      {
         GridRangeInfo rg;
         grid.Model.CoveredRanges.Find(rowIndex, colIndex, out rg);
         if (rg != null && grid.CurrentCell.HasCurrentCellAt(rg.Top, rg.Left))
            activeRange = rg;
      }
 
      GridRangeInfo rgCell = GridRangeInfo.Cell(rowIndex, colIndex);
 
      if (activeRange.Contains(rgCell))
          bSelEdge = true;
 
      GridQueryCanDragRangeEventArgs e = new GridQueryCanDragRangeEventArgs(activeRange);
                                                                 
      grid.RaiseQueryCanOleDragRange(e);
 
      if (bSelEdge && !e.Cancel)
       {
            grid.Model.CoveredRanges.Find(rowIndex, colIndex, out rgCovered);
return true;
       }
  }
   return false;
}

 

On MouseDown, invoke the method StartDragDrop of GridExcelLikeDragDropMouseController using Reflection to enable the drag and drop operation of the particular selected range. Please refer the code example for your reference

C#

public void MouseDown(MouseControllerEventArgs e)
{
   Point pt = new Point(e.Location.X, e.Location.Y);
  if (CheckHitTest(grid,pt) && e.Button == MouseButton.Left)
  {
     if (e.ClickCount == 1 || grid.Model.Options.ExcelLikeSelectionFrame)
     {
        var dragdrop = new GridExcelLikeDragDropMouseController(this.grid);
        dragDropFlags = grid.Model.Options.DragDropDropTargetFlags;
        dragdrop.EnableExcelLikeDragDrop(dragDropFlags);
        Type type = dragdrop.GetType();
        bool output = (bool)type.InvokeMember("StartDragDrop", BindingFlags.Instance |
                     BindingFlags.InvokeMethod | BindingFlags.NonPublic, null, dragdrop,
                                     new object[] { rgCovered.Top, rgCovered.Left });   
        if (output)
         this.grid.MouseControllerDispatcher.ActiveController = null;                              
      }
    }   
}

 

We have also prepared a sample for your reference,

WPF

 

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied