Solved

VB.net drag text onto an Excel cell

Posted on 2012-04-02
4
482 Views
Last Modified: 2012-04-03
Hi

In my VB.net Excel add-in I am using the following code to drag the text of a TreeView node into a DataGridView cell by first automatically finding which cell the mouse is over. Can this code be adapted to drop the text into the cell that the mouse is over in the Active spreadsheet


    Private Sub DataGridView1_DragEnter(sender As Object, e As System.Windows.Forms.DragEventArgs) Handles DataGridView1.DragEnter
        Try

            If e.Data.GetDataPresent("System.Windows.Forms.TreeNode", True) Then
                'TreeNode found allow move effect
                'e.Effect = DragDropEffects.Move
                e.Effect = DragDropEffects.All

            Else
                'No TreeNode found, prevent move
                e.Effect = DragDropEffects.None
                Exit Sub
            End If

        Catch ex As Exception

        End Try
    End Sub


    Private Sub DataGridView1_DragDrop(sender As Object, e As System.Windows.Forms.DragEventArgs) Handles DataGridView1.DragDrop
        Try

            'Check that there is a TreeNode being dragged
            If e.Data.GetDataPresent("System.Windows.Forms.TreeNode", _
                  True) = False Then Exit Sub

            'Get the TreeNode being dragged
            Dim dropNode As TreeNode = _
                  CType(e.Data.GetData("System.Windows.Forms.TreeNode"),  _
                  TreeNode)
            'TextBox1.Text = dropNode.Text

            Dim p As System.Drawing.Point = Me.DataGridView1.PointToClient(New System.Drawing.Point(e.X, e.Y))
            Dim hitTest As DataGridView.HitTestInfo = DataGridView1.HitTest(p.X, p.Y)

            If hitTest.RowIndex <> -1 AndAlso hitTest.ColumnIndex <> -1 Then
                'IF THERE IS A HIT
                Dim cell As DataGridViewCell = Me.DataGridView1(hitTest.ColumnIndex, hitTest.RowIndex)
                'Dim oRowIndex As Integer = hitTest.RowIndex
                Dim oColumnIndex As Integer = hitTest.ColumnIndex
                'MsgBox(CStr(oColumnIndex))
                DataGridView1.Rows(0).Cells(oColumnIndex).Value = dropNode.Text
            End If


        Catch
            MsgBox("There was a problem with the drag and drop ")
        End Try
    End Sub
0
Comment
Question by:murbro
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 37801974
Because Excel cell is beyond your control, you would need to put node text on the drag rather than the node itself as Excel wont be able to understand node.
0
 

Author Comment

by:murbro
ID: 37801990
How would I handle the drop event onto the spreadsheet. Do I add a handler?
0
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 500 total points
ID: 37802035
Excel will handle it as long as its plain text.
0
 

Author Closing Comment

by:murbro
ID: 37803026
Thanks very much
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

737 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question