This is a migrated thread and some comments may be shown as answers.

Copy a Cell throws an exception

2 Answers 440 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Rob
Top achievements
Rank 2
Iron
Veteran
Iron
Rob asked on 23 Oct 2020, 12:59 AM

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

Sort by
0
Accepted
Dess | Tech Support Engineer, Principal
Telerik team
answered on 27 Oct 2020, 10:23 AM
Hello, Rob, 

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/.

0
Rob
Top achievements
Rank 2
Iron
Veteran
Iron
answered on 28 Oct 2020, 01:48 AM

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

Patrick
Top achievements
Rank 1
commented on 14 Dec 2022, 02:35 PM

I'm just adding to this thread a note that I observe the same exception in 2022 using VS2022 Pro v17.4.0 with .Net Framework v4.8 (target v4.5) and Telerik WinForms v4.0.30319

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)
Dess | Tech Support Engineer, Principal
Telerik team
commented on 15 Dec 2022, 11:25 AM

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.

Patrick
Top achievements
Rank 1
commented on 15 Dec 2022, 01:28 PM

Hi Dess,

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. 
Dess | Tech Support Engineer, Principal
Telerik team
commented on 16 Dec 2022, 08:32 AM

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);
            } 
        }

 

Patrick
Top achievements
Rank 1
commented on 19 Dec 2022, 02:00 PM

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!

Dess | Tech Support Engineer, Principal
Telerik team
commented on 19 Dec 2022, 02:14 PM

Patrick, I am really glad to hear that the suggested solution works for your scenario. We will consider creating an appropriate resource for handling this specific scenario so other customers can benefit from it. For now, after discussing this case with the team, we don't think that this should be handled internally by our RadGridView as it seems to be a more global problem.
Patrick
Top achievements
Rank 1
commented on 27 Mar 2023, 06:04 PM | edited

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 });
The second Boolean parameter fixed it, but I don't know what that second parameter actually does.
Dess | Tech Support Engineer, Principal
Telerik team
commented on 28 Mar 2023, 04:42 AM

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 

Tags
GridView
Asked by
Rob
Top achievements
Rank 2
Iron
Veteran
Iron
Answers by
Dess | Tech Support Engineer, Principal
Telerik team
Rob
Top achievements
Rank 2
Iron
Veteran
Iron
Share this question
or