DataGridView InvalidOperationException重入对SetCurrentCellAddressCore的调用 [英] DataGridView InvalidOperationException reentrant call to SetCurrentCellAddressCore

查看:44
本文介绍了DataGridView InvalidOperationException重入对SetCurrentCellAddressCore的调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经为此工作了大约8个月.直到最近,当我从DataSet/DataTables移到列表时,这才是最令人烦恼的事情.现在问题变得更加普遍了(我认为是因为列表似乎更有效).

I've been working on this for about 8 months. It's been an annoyance more than anything until recently when I moved from a DataSet / DataTables to lists. Now the problem is a lot more prevalent (I think because the lists appear to be a LOT more efficient).

已经问过几次这个问题,但是没有一个人真正地询问了正在发生的事情(也没有一个人回答).奇怪的是,我无法隔离在代码中导致异常的地方,因为调试器会拉出只有以下代码的program.cs:

This question has been asked a few times but none of them really hit on what truly is going on (nor are any of them answered). The odd thing is I can't isolate where in my code this is causing the exception as the debugger pulls up the program.cs which only has this code:

    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new MyApp());

    }

应用程序异常在Application.Run ...行上.

The application exception is on the Application.Run... line.

我正在使用DataGridView作为后台处理日志显示.我在服务中有许多后台流程,这些后台流程可以与Winform应用通讯.该表单侦听这些消息(事件处理程序/信号器),并删除消息(例如FIFO队列),使其不超过列表中的最大定义数量,然后处理该消息并将其分类为BindingList [].然后在datagrid中,如果我单击一个项目,它将在文本框中显示完整的消息.我再次关闭了多选功能,datagridview是只读的.

I'm using the DataGridView as a back ground processing log display. I have numerous background processes in a service that communicate back up to a winform app. The form listens for these messages (event handler / signalr) and also remove messages (like a FIFO queue) to not exceed a maximum defined amount in a list then I process the message and sort them into a BindingList[]. Then in the datagrid if I click on an item, it will display the full message in a textbox. I have multiselect turned off and again, the datagridview is read only.

哦,同样,BindingList []从另一个控件绑定到/绑定到datagrid,所以我可以选择要在datagridview中显示的列表.这不是问题,因为我通过在代码中强制使用单个特定列表来解决该问题,但仍然存在问题.

Oh also, the BindingList[] is bound / rebound to the datagrid from another control so I can select which list to display in the datagridview. This is not the issue as I've isloated the issue by forcing a single specific list in the code and still have the problem.

要使其崩溃,我可以多次单击datagrid,最终它将崩溃.如果我真的想使其快速崩溃,请单击datagridview并向上和向下滚动(键盘箭头),然后在几秒钟内将其崩溃.

To get this to crash, I can click on datagrid numerous times and eventually it will crash. If I really want to crash it quickly, I click on the datagridview and scroll (keyboard arrow) down and up and I can crash it in a few seconds.

我发现此关于StackOverflow的文章(单击此处),描述了正在发生的事情.在其中一项注释中,它引用了 Microsoft错误报告(单击此处)这是设计使然!但是,大多数人都在谈论操纵我未使用的单元.最上面的消息几乎与我发生的事情相同,但是程序员使用的是继承的DataGridView,因此他的解决方案对我不起作用.

I found this article (click here) on StackOverflow that describes what is going on. And in one of the comments it refers to a Microsoft Bug Report(click here) which stats this is by design! However, most are talking about manipulating the cells which I am not doing. The top message is nearly identical to what is happening to me but the programmer is using an inherited DataGridView so his solution will not work for me.

这确实与在BindingList中添加和/或删除项目有关.如果在DataGridView中滚动/选择时发生任何一种情况,我可能会使其崩溃.但是该代码非常简单:

This does have to do with adding and or deleting items from the BindingList. I can get it to crash if I have either of the going on while scrolling / selecting in the DataGridView. But that code is very simple:

    private void DelRow( string szTableName)
    {
        try
        {
            int nProcQueue = qdList.Queue(szTableName);
            MsgQueues[nProcQueue].RemoveAt(0);

            this.BeginInvoke(new MethodInvoker(Refresh_dgvDetail));
        }
        catch (Exception ex) 
        {
            LogEx(ex); 
        }
    }

    private void AddRow(LogObject oLogObject, string szTableName)
    {
        try
        {
            int nQueueNumber = qdList.Queue(szTableName); // helper object to return queue number based off the name of the list

            MsgQueues[nQueueNumber].Add(oLogObject);


            this.BeginInvoke(new MethodInvoker(Refresh_dgvDetail));
        }
        catch (Exception ex) 
        { 
           LogEx(ex);
        }
    }

这似乎确实是一个c#错误...为什么MS会按我的设计进行设计……?

This really seems like a c# bug... Why MS would have this as designed is beyond me...?

有人知道如何停止这种行为吗?

Anyone know how to stop this behavior?

推荐答案

Grek40,您是对的;我错了.我只是添加了MethodInvoker.不是删除.两者都必须完成.基本上,任何与datagridview接触的方法都需要具有MethodInvoker.这是我所做的一个例子:

Grek40, you're right; I was wrong. I did the MethodInvoker for just the add; not the delete. It had to be done for both. Basically anything method that touches the datagridview needs to have MethodInvoker. This is an example of what I did:

this.Invoke((MethodInvoker)delegate {MsgQueues [nCurrentQueue] .RemoveAt(0);});

this.Invoke((MethodInvoker)delegate { MsgQueues[nCurrentQueue].RemoveAt(0); });

问题消失了.

这篇关于DataGridView InvalidOperationException重入对SetCurrentCellAddressCore的调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆