C# DragDrop 注册失败 的处理方法之一

来源:互联网 发布:乐视电视软件 编辑:程序博客网 时间:2024/05/22 03:45

System.InvalidOperationException: DragDrop 注册失败。 ---> System.Threading.ThreadStateException: 在可以调用 OLE 之前,必须将当前线程设置为单线程单元(STA)模式。请确保您的 Main 函数带有 STAThreadAttribute 标记。

   在 System.Windows.Forms.Control.SetAcceptDrops(Boolean accept)
   --- 内部异常堆栈跟踪的结尾 ---
   在 System.Windows.Forms.Control.SetAcceptDrops(Boolean accept)
   在 System.Windows.Forms.Control.OnHandleCreated(EventArgs e)
   在 System.Windows.Forms.DataGridView.OnHandleCreated(EventArgs e)
   在 System.Windows.Forms.Control.WmCreate(Message& m)
   在 System.Windows.Forms.Control.WndProc(Message& m)
   在 System.Windows.Forms.DataGridView.WndProc(Message& m)
   在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)

   在 System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)


网上找了一圈,试了所有的方法,都不能解决我这个问题,于是静下心来,仔细查看异常堆栈信息,发现异常是从DataGridView中传出来的,结合.SetAcceptDrops失败,可以知道该问题与DataGridView中的相关属性有关, 于是查一下属性,发现AllowDrop为true,改为false,再次运行,异常清除。

原创粉丝点击