Hi Guys
I can't work out why , but every cell in every grid in my application ( different forms and Grids) all throw an exception when you try to copy a cell.
This happens for both the right click, select copy , or with Ctrl C
System.Runtime.InteropServices.COMException occurred
Message: Exception thrown: 'System.Runtime.InteropServices.COMException' in System.Windows.Forms.dll
Additional information: Invalid FORMATETC structure (Exception from HRESULT: 0x80040064 (DV_E_FORMATETC))
Been googling like mad , found the info about copy events for the grid. These seem to fire (3 times each for each format) and then the exception happens. Even calling cancel in the event or trying to format the value returned still causes the exception.
I am using version v4.0.30319 , but I don't see any fixes in later versions for this.
Some trouble shooting ideas would be greatly appreciated
Regards
Rob
2 Answers, 1 is accepted
RadGridView introduces Copying event which occurs when the grid has prepared appropriate data formats that represent the copy selection. This event is fired once for each supported format:Text, HTML, CommaSeparatedValue. You can cancel this event if the data is not allowed to be stored to Clipboard in a specific format, e.g. HTML format.
According to the provided information, it wouldn't be easy to determine how to replicate the issue locally and whether the RadGridView control actually causes it. Could you please specify the exact steps how to reproduce the problem or get back to me with a sample project so I can investigate the precise case? I believe that the same behavior will be observed with the MS DataGridView.
After some research in general programming forums, it seems that it is a common problem. Please refer to the following threads which are quite useful on this topic:
https://stackoverflow.com/questions/5093264/cryptic-exception-copy-pasting-from-datagridview-into-excel-2002
https://social.msdn.microsoft.com/Forums/windows/en-US/0cb6c5a9-524e-4725-83c6-09853b2f6da5/setdataobject-troubles?forum=winforms
https://developercommunity.visualstudio.com/content/problem/751272/exception-on-clipboardsetdataobject.html
I hope this information helps. If you need any further assistance please don't hesitate to contact me.
Regards,
Dess | Tech Support Engineer, Sr.
Progress Telerik
Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.
Hey Dess
I do have a MS DataGridView in the same application ( shocking I know!) and that does not throw an exception when copying a cell. However , the data never appears in the clip board.
After reading those links ( your google skills are clearly superior) , I tried building a new project from scratch with nothing but a RadGrid with a few columns and nothing more. If I run this in VS , add a row of data and try the copy.. same exception.
So I tried the compiled file on another pc and it worked fine.. then I finally tried running the compiled file on my development machine and it works fine.
So it seems to be a VS issue and I am sure something specific to this development machine.
Thank you for the assistance.
Rob
Exception is thrown when attempting to copy a cell value from RadGridView when it's in non-edit mode and while running app in debug in VS2022.
Invalid FORMATETC structure (Exception from HRESULT: 0x80040064 (DV_E_FORMATETC))
at System.Runtime.InteropServices.Marshal.ThrowExceptionForHRInternal(Int32 errorCode, IntPtr errorInfo)
at System.Windows.Forms.DataObject.GetDataIntoOleStructs(FORMATETC& formatetc, STGMEDIUM& medium)
Hi, Patrick,
I am really sorry to hear that you are experiencing any difficulties with the copy functionality in RadGridView. However, according to the stack trace it wouldn't be easy to determine what causes this undesired behavior. Could you please elaborate? Could you please specify the exact steps how to reproduce the problem? Alternatively, you can submit a support ticket from your Telerik account and provide a sample runnable project demonstrating the problem you are facing. Thus, we would be able to make an adequate analysis of the precise case and provide further assistance. Thank you.
The purpose of my comment was to make a note that this situation continues to come up in some circumstances years after it was originally reported by Rob. Just like for the other person this so far seems to happen only while debugging in Visual Studio session. I haven't observed this error when an app is compiled in Release mode and runs as a stand-alone executable.
My call stack when exception is thrown from Systems.Windows.Forms.dll
> System.Windows.Forms.dll!System.Windows.Forms.DataObject.GetDataIntoOleStructs(ref System.Runtime.InteropServices.ComTypes.FORMATETC formatetc, ref System.Runtime.InteropServices.ComTypes.STGMEDIUM medium) Unknown
System.Windows.Forms.dll!System.Windows.Forms.DataObject.System.Runtime.InteropServices.ComTypes.IDataObject.GetDataHere(ref System.Runtime.InteropServices.ComTypes.FORMATETC formatetc, ref System.Runtime.InteropServices.ComTypes.STGMEDIUM medium) Unknown
System.Windows.Forms.dll!System.Windows.Forms.DataObject.System.Runtime.InteropServices.ComTypes.IDataObject.GetData(ref System.Runtime.InteropServices.ComTypes.FORMATETC formatetc, out System.Runtime.InteropServices.ComTypes.STGMEDIUM medium) Unknown
I bind RadGridView to an ObservableCollection of objects of a custom class. I think that perhaps my class doesn't implement some uncommon Interface that is expected by this ComTypes.FORMATETC that complains. However, this is just me theorizing, and I actually have no idea.
If one day Telerik finds the reason that it would be nice if you post the solution here, as this ticket is what comes up on top of search results when I looked for this exception and GridView.
At this time I don't need to open my own ticket as I'm not looking for an immediate solution.
OK, Patrick. Thank you for sharing this with the community. However, this error doesn't seem to be related directly to the RadGridView control from the Telerik UI for WinForms suite. When a Copy operation is performed, RadGridView internally uses Clipboard.SetDataObject(Object) method. In the different general programming forums, someone confirmed that if the SetDataObject(Object, Boolean) method overload is used, the error doesn't occur on his end. Could you please confirm whether this has a positive impact on your machine as well? I have prepared a sample code snippet for your reference demonstrating how to customize the copy behavior in RadGridView:
public class CustomGrid : RadGridView
{
public override string ThemeClassName
{
get
{
return typeof(RadGridView).FullName;
}
}
protected override RadGridViewElement CreateGridViewElement()
{
return new CustomRadGridViewElement();
}
}
public class CustomRadGridViewElement : RadGridViewElement
{
protected override Type ThemeEffectiveType
{
get
{
return typeof(RadGridViewElement);
}
}
protected override MasterGridViewTemplate CreateTemplate()
{
return new CustomMasterGridViewTemplate();
}
}
public class CustomMasterGridViewTemplate : MasterGridViewTemplate
{
public override void Copy()
{
MethodInfo mi = typeof(MasterGridViewTemplate).GetMethod("CopyContent", BindingFlags.Instance | BindingFlags.NonPublic);
object copyResult = mi.Invoke(this, new object[] { false });
Clipboard.SetDataObject(copyResult, true);
}
}
Hi Dess,
I had a chance today to try your suggested code, and it immediately resolved the issue. No longer any exceptions are thrown when I try to copy values from columns of any datatype in RadGridView while running in Visual Studio debug mode.
Thank you for going beyond the expected and helping out your customers and the community. You're wonderful!
Last week I upgraded our code to .Net Framework 4.8.2 from 4.5.2 and started getting a new Exception
System.Reflection.TargetParameterCountException
HResult=0x8002000E
Message=Parameter count mismatch.
Source=mscorlib
StackTrace:
at System.Reflection.RuntimeMethodInfo.InvokeArgumentsCheck(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
This is thrown by this line
object copyResult = mi.Invoke(this, new object[] { false });
in CustomMasterGridViewTemplate.Copy(). I resolved it by pure luck, by trying to add more parameters.
object copyResult = mi.Invoke(this, new object[] { false, false });
Hi, Patrick,
I am sorry to hear that you are experiencing any difficulties with RadGridView.
Note that all UI controls are not thread safe controls in the whole Windows Forms platform (not just Telerik controls, but all controls out there). Here is an article on MSDN, describing how to make thread-safe Winforms UI application. This means that any control from the Telerik UI for WinForms suite is not thread safe as well and cannot be used outside the main UI thread. You should use an Invoke to update the controls in cross threading scenario.
Usually, such an error is possible to occur when the invoked method doesn't get all the parameters passed:
private DataObject CopyContent(bool cut, bool copyToClipboard = true)
The following MSDN thread discusses a similar topic as well: https://social.msdn.microsoft.com/Forums/vstudio/en-US/dc142c3e-05a6-4862-8f2d-22f4dc504c11/how-to-resolve-the-exception-quotparameter-count-mismatchquot-is-thrown-from?forum=csharpgeneral