C# .NET WinForms How do I copy instance of class from one List<class> to another List<class> using Clipboard

0

I'm writing a C# .NET WinForms app which contains two DataGridView controls, each with its own List:

DataGridView1, List1 DataGridView2, List2

FYI: DataItem is a class consisting of integers, strings, booleans, etc., and all items listed in each datagridview control are also listed in its corresponding List.

The user will right-click on a DataGridView1 and in the resulting context menu, click "Copy Item" to copy a DataItem from List1 to the clipboard...

    private void ctxtContextMenu_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
    {
        int SelectedRowIndex = DataGridView1.SelectedRows[0].Index;
        if (ctxtCopyItem == e.ClickedItem)
        {
            Clipboard.SetDataObject(List1[SelectedRowIndex]);
        }
        else if (ctxtPasteItem == e.ClickedItem)
        {
            PasteDataItem();
        }
     }

The user will then go to DataGridView2, right-click on it and in the context menu, click "Paste Item" to add that DataItem to DataGridView2 and to List2...

    private void PasteDataItem()
    {
        DataItem dataitem = new DataItem();
        dataitem = Clipboard.GetDataObject());   // This line is where the error occurs.
        List2.Add(dataItem); 
        UpdateDataGridView2();
     }

And here's the code for the context menu's Opening EventHandler...

    private void ctxtContextMenu_Opening(object sender, CancelEventArgs e)
    {
        // If clipboard contains a DataItem, enable ctxtPasteItem...
        if (Clipboard.GetDataObject() is DataItem)
            ctxPasteQuestion.Enabled = true;
    }

In the method "PasteDataItem()" above, an exception occurs:

System.InvalidCastException occurred HResult=0x80004002 Message=Unable to cast object of type 'System.Windows.Forms.DataObject' to type 'DataItem'.

Apparently, I'm either not copying DataItem to the clipboard correctly or I'm not retrieving it from the clipboard correctly. One possible cause is that I'm not casting the DataItem properly. I haven't found any online tutorials on casting, copying and pasting an instance of a class using the clipboard. Is this even possible and if so, what's the right way to get that class instance to and from the Clipboard?

c#
.net
winforms
datagridview
clipboard
asked on Stack Overflow Jul 17, 2017 by manicdrummer

1 Answer

0

Clipboard.GetDataObject() returns an IDataObject and not your stored data directly. Then, you can use that returned IDataObject to access your stored data. Potentially what you want is the following:

var clipboardDataObject = Clipboard.GetDataObject();
dataitem = (DataItem)clipboardDataObject.GetData(typeof(DataItem));

Although ideally you'll make that code more robust to handle errors and such. Additionally, this means that your code on ctxtContextMenu_Opening may not be working as expected either.

answered on Stack Overflow Jul 17, 2017 by Fredy Treboux

User contributions licensed under CC BY-SA 3.0