Articles in this section
Category / Section

How to retrieve the current row and column while using drag and drop in WinForms GridControl?

2 mins read

Drag and drop

While using the DragDrop event, you can get the dragged data and the dropped value points X and Y positions. From these points, use the PointToRowCol() to get the row and column index.

C#

this.treeview.ItemDrag += new System.Windows.Forms.ItemDragEventHandler(this.treeView_ItemDrag);
this.treeview.DragEnter += new System.Windows.Forms.DragEventHandler(this.treeView_DragEnter);
this.grid.DragDrop += grid_DragDrop;
void grid_DragDrop(object sender, DragEventArgs e)
{
   //allow the Drag from Treenode and drop it to Grid
   if (e.Data.GetDataPresent("System.Windows.Forms.TreeNode", false))
   {
      Point pt = ((GridControl)sender).PointToClient(new Point(e.X, e.Y));
      int row;
      int col;
      // to retrieve the current row and column number where the data is being dropped.
      this.grid.PointToRowCol(pt, out row, out col);
      MessageBox.Show("Row = " + row + "\t" + "Column = " + col);
   }
}
private void treeView_ItemDrag(object sender,
System.Windows.Forms.ItemDragEventArgs e)
{
   //enables the items to move
   DoDragDrop(e.Item, DragDropEffects.Move);
}
private void treeView_DragEnter(object sender,
System.Windows.Forms.DragEventArgs e)
{
   //allow the drag support to move
   e.Effect = DragDropEffects.Move;
}

VB

Private Me.treeview.ItemDrag += New System.Windows.Forms.ItemDragEventHandler(AddressOf Me.treeView_ItemDrag)
Private Me.treeview.DragEnter += New System.Windows.Forms.DragEventHandler(AddressOf Me.treeView_DragEnter)
Private Me.grid.DragDrop += AddressOf grid_DragDrop
Private Sub grid_DragDrop(ByVal sender As Object, ByVal e As DragEventArgs)
   'allow the Drag from Treenode and drop it to Grid
   If e.Data.GetDataPresent("System.Windows.Forms.TreeNode", False) Then
      Dim pt As Point = (CType(sender, GridControl)).PointToClient(New Point(e.X, e.Y))
      Dim row As Integer
      Dim col As Integer
      'To retrieve the current row and column number where the data is being dropped.
      Me.grid.PointToRowCol(pt, row, col)
      MessageBox.Show("Row = " & row + Constants.vbTab & "Column = " & col)
   End If
End Sub
Private Sub treeView_ItemDrag(ByVal sender As Object, ByVal e As System.Windows.Forms.ItemDragEventArgs)
   'enables the items to move
   DoDragDrop(e.Item, DragDropEffects.Move)
End Sub
Private Sub treeView_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs)
   'allow the drag support to move
   e.Effect = DragDropEffects.Move
End Sub

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