Click here to Skip to main content
15,891,981 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
I am trying to get a handle on how to drag and drop an image file from Windows File Explorer into a DataGridView cell in a Windows Form.

The basic function of dropDrop works but I don't know what the cell to drop into is.
So I try go get the cell column/row from other events but it goes into the wrong cell.

What I seen is to use HitTest in the DaaGriveView DragDrop event but the hittest type is always none so the column and row index is always -1. (The type should be cell).

So what is the proper method to Drag a image file from Windows File Explorer into a cell in a DataGridView?

Thanks

What I have tried:

Using HitTest but the type is always none
Using other events to get the last good cell column/row.
Posted
Updated 17-Apr-18 5:14am
v2
Comments
Maciej Los 16-Apr-18 14:40pm    
What have you tried?

Quote:
Using HitTest but the type is always none

DataGridView.HitTest Method (Int32, Int32) (System.Windows.Forms)[^] returns location information, such as row and column indices, given x- and y-coordinates.

Well, you didn't mention about the way you get mouse coordinates... So, please check this: c# - Datagridview context menu always shows -1 in hittest[^] to get more details about your issue.


Here's an article: Drag and Drop Text from One to Other Datagrirdview in C# Windows Application[^] which might be helpful in resolving your issue.
 
Share this answer
 
v2
Comments
QuickBooksDev 17-Apr-18 6:47am    
Here is the DropDrag code seqment but it is from e.x and e.y. I will review the other posting but this is from an external source (File Explorer) not another cell.
QuickBooksDev 17-Apr-18 8:16am    
Private Sub DataGridView1_DragDrop(sender As Object, e As DragEventArgs) Handles DataGridView1.DragDrop
Dim C, R as integer
...
Dim myDGV As DataGridView = sender
        Dim HtInfo As DataGridView.HitTestInfo = myDGV.HitTest(e.X, e.Y)
        If HtInfo.Type = DataGridViewHitTestType.Cell Then
            C = HtInfo.ColumnIndex
            R = HtInfo.RowIndex
            DropColNo = C : DropRowNo = R
            lblDebug.Text &= " Hit=" & C & "," & R & " "
            myUseCol = C : myUseRow = R
            'DataGridView1.CurrentCell = DataGridView1(C, R)
            Debug.WriteLine("Drag Drop Current HIT Cell " & C & "," & R)
            GoTo DoPic
        Else
            C = HtInfo.ColumnIndex : R = HtInfo.RowIndex
            Debug.WriteLine("Drag Drop Current Bad HT=" & C & "," & R)
            lblDebug.Text &= " Invalid type " & HtInfo.Type.ToString()
            If DropColNo > -1 AndAlso DropRowNo > -1 Then
                myUseCol = DropColNo : myUseRow = DropRowNo
                'DataGridView1.CurrentCell = DataGridView1(DropColNo, DropRowNo)
                Debug.WriteLine("Drag Drop bad Hit Cell " & DropColNo & "," & DropRowNo & " " & HtInfo.Type.ToString())
            Else
                lblDebug.Text &= " invalid drop=" & DropColNo & "," & DropRowNo & " "
                Debug.WriteLine("Drag Drop bad dropCol/Row invalid drop=" & DropColNo & "," & DropRowNo & " ")
            End If
        End If
...
End Sub
Maciej Los 17-Apr-18 12:04pm    
You have to handle 3 events: CellMouseDown, DragEnter, DragDrop, but you're using only one. Follow this: How to provide file drag-and-drop functionality in a Visual C# application and change to your needs.
It looks like that other posting has the solution and using
Point p = dataGridView2.PointToClient(new Point(e.X, e.Y);
DataGridView.HitTestInfo info = dataGridView2.HitTest(p.X, p.Y);


Solved the problem.

Thanks
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900