后台线程更新DataGridView数据的一个异常更改

 

程序中用到一个线程不停的更新主界面上的一个DATAGRIDVIEW,更新时,经常出现如下 错误:

Object reference not set to an instance of an object..
   at System.Windows.Forms.DataGridViewRow.GetErrorText(Int32 rowIndex)
   at System.Windows.Forms.DataGridViewRow.Paint(Graphics graphics, Rectangle clipBounds, Rectangle rowBounds, Int32 rowIndex, DataGridViewElementStates rowState, Boolean isFirstDisplayedRow, Boolean isLastVisibleRow)
   at System.Windows.Forms.DataGridView.PaintRows(Graphics g, Rectangle boundingRect, Rectangle clipRect, Boolean singleHorizontalBorderAdded)
   at System.Windows.Forms.DataGridView.PaintGrid(Graphics g, Rectangle gridBounds, Rectangle clipRect, Boolean singleVerticalBorderAdded, Boolean singleHorizontalBorderAdded)
   at System.Windows.Forms.DataGridView.OnPaint(PaintEventArgs e)
   at System.Windows.Forms.Control.PaintWithErrorHandling(PaintEventArgs e, Int16 layer, Boolean disposeEventArgs)
   at System.Windows.Forms.Control.WmPaint(Message& m)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.DataGridView.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
   at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
   at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
   at System.Windows.Forms.Application.Run(Form mainForm)
   at HWPT.TrackingStation.Program.Main() in D:/KJ297/TrackingStation/Program.cs:line 122

根据DataGridView的事件 DataError 可以捕获取是 Display 时的错误。因此基本确定时线程与主界面线程资源处理的问题。大概的假设就是:后台已经删除了一条记录,而前台主界面线程又去刷新显示实际上已经被删除的记录。从而导致异常。

那后台线程不能删除,就移到前台线程中来删除了:) 使用委托可以实现这个目的:

 

         // 定义一个委托
        delegate   void  dlgtGenePara1( int  iValue);
       
// 这里用于DataGridView的删除等操作
         void  DelRoute( int  iRoute)
        
{
            DataView dv 
= dgvRoutes.DataSource as DataView;//dgvRoutes 为一个DataGridView 对象
            lock (dv)
            
{
                
for (int i = 0; i < dv.Count; ++i)
                
{
                    
if ((dv[i]["obj"as CRouteMoniteringMgr.CItem).route.ID == iRoute)
                    
{
                        dv[i].Delete();
                        
break;
                    }

                }

            }

            dgvRoutes.Refresh();
        }

在后台线程事如果需要进行删除更新等操作,则直接使用委托调用即可。

--------------------------------------------------------------------------------------------------------

在程序中进行这么更改后,原先的异常目前暂时不再发生,因此基本断定就是这个原因导致的。

欢迎大家对此补充。

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值