)
We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. (Last updated on: June 24, 2019).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

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

Platform: WinForms |
Control: GridControl |
Published Date: April 21, 2015 |
Last Revised Date: December 20, 2019

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

Samples:

C#: DragDrop

VB: DragDrop

2X faster development

The ultimate WinForms UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile
Live Chat Icon