后台线程更新DataGridView数据的一个异常更改
来源:互联网 发布:ibm服务器安装linux 编辑:程序博客网 时间:2024/06/02 09:31
程序中用到一个线程不停的更新主界面上的一个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();
}
在后台线程事如果需要进行删除更新等操作,则直接使用委托调用即可。
--------------------------------------------------------------------------------------------------------
在程序中进行这么更改后,原先的异常目前暂时不再发生,因此基本断定就是这个原因导致的。
欢迎大家对此补充。
- 后台线程更新DataGridView数据的一个异常更改
- IOS后台线程更新UI的一个陷阱
- IOS后台线程更新UI的一个陷阱
- C#中DataGridView的数据批量更新
- mysql(workbench)更新数据时候的一个异常
- DataGridView 更新数据
- dataGridView数据更新
- DataGridView显示、更新数据
- OleDbDataAdapter 更新dataGridView数据
- DataGridView数据同步更新
- 更改绑定数据源的DataGridView的数据排序
- 实现datagridview中数据的添加,更改,删除功能
- 后台线程更新界面的巧妙方法
- 【Android笔记-异常-4】定义一个临时的数组变量承接数据,ListView的数据以及通知数据更新要放到同一个线程(主线程)。避免出现异常"The content of the adapter
- Android 更改一个更新
- datagridview的使用,即时新增,更新,删除数据库数据
- dataGridView 批量更新数据 内存溢出的解决方案
- HandlerThread 创建一个异步的后台线程
- 《大规模C++程序设计》读书笔记31
- Singleton在C++中的两种实现方法
- 肯德基狂整一个小男生
- 浣溪沙.闲情偶寄
- 拥有微软Windows CE的实时系统
- 后台线程更新DataGridView数据的一个异常更改
- 寻找循环链表的循环起始点
- 完美助手 FinePlus 1.43.5 正式版
- 连接oracle的总结
- 论Sun的学究思路
- JSP连接mysql数据库
- Windows CE 编程的十点忠告
- 关于for(输出图形)
- 用户权限设计