<p>I'm trying to implement Drag &amp; Drop functionality with source being a TreeView control. When I initiate a drag on a node, I'm getting:</p>

<p><em>Invalid FORMATETC structure (Exception from HRESULT: 0x80040064 (DV_E_FORMATETC))</em></p>

<p>The ItemDrag handler (where the exception takes place), looks like:</p>

<pre><code>private void treeView_ItemDrag(object sender,
        System.Windows.Forms.ItemDragEventArgs e)
{

        this.DoDragDrop(e.Item, DragDropEffects.Move);
}
</code></pre>

<p>Does anyone know the root cause of this and how to remedy it? (.NET 2.0, Windows XP SP2)</p>

[{'answer_id': 52030, 'author': 'Stradas', 'author_id': 5410, 'author_profile': 'https://Stackoverflow.com/users/5410', 'pm_score': 1, 'selected': False, 'text': "<p><strong><code>FORMATETC</code></strong> is a type of application clipboard, for lack of a better term.  In order to pull off some of the visual tricks of draging around the tree node, it has to be copied into this clipboard with its source description.  The source control loads its info into the <code>FORMATETC</code> clipboard and sends it to the target object.  It looks like the error occurs on the drop and not on the drag. The <code>DV</code> in <code>DV_E_FORMATETC</code> typically indicates the error occurrs on the drop step.<br>\nThe destination doesn't look like it likes what you are droping on it. The clipboard may be corrupt or the drop destination may not be configured to understand it.  </p>\n\n<p>I recommend you try one of two things.  </p>\n\n<ol>\n<li>Remove the original tree structure and destination. Dump your dlls.  Close everything.  Open up and put the treeview and destination back on the form.  It may have just been poorly formed and not fully populating the <code>FORMATETC</code> structure.</li>\n<li>Try putting another treeview and droping to that.  If you are droping to another tree and it works you know your oranges to oranges work and it isn't the treeview.  It may be the destination if it is a grid or listview.  You may need to change those structures to be able to receive the drop.</li>\n</ol>\n\n<p>Not that it helps but the structure is something like this:  </p>\n\n<pre><code>typedef struct tagFORMATETC\n{\n  CLIPFORMAT      cfFormat;\n  DVTARGETDEVICE  *ptd;\n  DWORD           dwAspect;\n  LONG            lindex;\n  DWORD           tymed;\n} FORMATETC, *LPFORMATETC;\n</code></pre>\n"}, {'answer_id': 1315206, 'author': 'Joe Caffeine', 'author_id': 159770, 'author_profile': 'https://Stackoverflow.com/users/159770', 'pm_score': 1, 'selected': False, 'text': '<p>When doing drag and drop with list and treeview controls you have to make sure that you removing and inserting the list items correctly. For example, using drag and drop involving three ListView controls:</p>\n\n<pre><code>    private void triggerInstanceList_DragOver(object sender, DragEventArgs e)\n    {\n        SetDropEffect(e);\n    }\n\n    private void triggerInstanceList_DragEnter(object sender, DragEventArgs e)\n    {\n        SetDropEffect(e);\n    }\n\n    private void SetDropEffect(DragEventArgs e)\n    {\n        if (e.Data.GetDataPresent(typeof(ListViewItem)))\n        {\n            ListViewItem itemToDrop = e.Data.GetData(typeof(ListViewItem)) as ListViewItem;\n            if (itemToDrop.Tag is TriggerTypeIdentifier)\n                e.Effect = DragDropEffects.Copy;\n            else\n                e.Effect = DragDropEffects.Move;\n        }\n        else\n            e.Effect = DragDropEffects.None;\n    }\n\n    private void triggerInstanceList_DragDrop(object sender, DragEventArgs e)\n    {\n        if (e.Data.GetDataPresent(typeof(ListViewItem)))\n        {\n            try\n            {\n                ListViewItem itemToDrop = e.Data.GetData(typeof(ListViewItem)) as ListViewItem;\n                if (itemToDrop.Tag is TriggerTypeIdentifier)\n                {\n                    ListViewItem newItem = new ListViewItem("&lt;new " + itemToDrop.Text + "&gt;", itemToDrop.ImageIndex);\n                    _triggerInstanceList.Items.Add(newItem);\n                }\n                else\n                {\n                    _expiredTriggers.Items.Remove(itemToDrop);\n                    _triggerInstanceList.Items.Add(itemToDrop);\n                }\n            }\n            catch (Exception ex)\n            {\n                Debug.WriteLine(ex);\n            }\n        }\n    }\n</code></pre>\n\n<p>you will note that at the end of the DragDrop event I am either moving the ListViewItem or creating a copy of one.</p>\n'}, {'answer_id': 10566072, 'author': 'Steve Cadwallader', 'author_id': 41693, 'author_profile': 'https://Stackoverflow.com/users/41693', 'pm_score': 2, 'selected': False, 'text': '<p>In case it helps anyone else - I encountered this problem with the WPF TreeView (not Windows Forms as listed in the question) and the solution was simply to make sure to mark the event as handled in the drop event handler.</p>\n\n<pre><code>    private void OnDrop(object sender, DragEventArgs e)\n    {\n        // Other logic...\n\n        e.Handled = true;\n    }\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/50332', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/4398/']