I’m having an issue involving the following scenario, Application A has a DataGridView, from which objects are selected. They are dragged into a DataGridView inside Application B, and the objects are copied into the new list. Here is the code in App A, preparing the dragdrop:
ArrayList ToDrag = this.GetSelectedBoundItems();
DataObject data = new DataObject(ToDrag);
this.DoDragDrop(data, DragDropEffects.Move | DragDropEffects.Copy);
The problem: the recieving end is unable to get the ArrayList. Here is the code for that:
//formats has the value of "System.Collections.ArrayList"
var formats = myRetrievedObject.GetFormats();
//candoit has the value "true"
var candoit = myRetrievedObject.GetDataPresent(typeof(ArrayList));
//DraggedItems is of type "System.__ComObject"
var DraggedItems = myRetrievedObject.GetData(typeof(ArrayList));
//this returns null. I 'think' this should work
ArrayList DraggedItems2 = myRetrievedObject.GetData(typeof(ArrayList)) as ArrayList;
//This throws an exception (see below)
ArrayList DraggedItems2 = (ArrayList)myRetrievedObject.GetData(typeof(ArrayList)) as ArrayList;
This used to work. Two things have happened to this, we jumped from .net 2.0 to .net 4.0, and this code was translated into C++.net into C#.
I’m not sure what I’m missing. That data is supposed to be translated into an array list.
Thanks for the help!
edit this is the exception text from the cast
A first chance exception of type ‘System.InvalidCastException’ occurred in dfResultsControls.dll
Additional information: Unable to cast COM object of type ‘System.__ComObject’ to class type ‘System.Collections.ArrayList’. Instances of types that represent COM components cannot be cast to types that do not represent COM components; however they can be cast to interfaces as long as the underlying COM component supports QueryInterface calls for the IID of the interface.
Making the (data) object serializable solves the problem for me.