DataGridView 在多线程中使用可能出现大红叉,解决方法如下
来源:互联网 发布:折叠式摩托车淘宝 编辑:程序博客网 时间:2024/04/28 15:41
今天做了一个多线程实现 DataGridView 动态更新数据,但是在动态实现更新数据达到20次左右时,DataGridView 突然变成一个大红叉,通过查询,解决如下:
老外的解释:
The DataGridView is a common .Net control used to display and permit editing of tabular data. It can be filled in via code or by attaching a data source to it.
DataGridViews, like most controls, are not thread-safe. That is, you need to perform operations on them using the same thread as they were created on. However, sometimes OpenSpan developers will accidently modify the DataGridView from another thread because the operation is coming from an event in another application.
A safe way to correct this problem and to ensure it doesn't accidently happen is to postpone updating the control during a OnPaint() event if it happens from another thread. This is done by handling an exception and then flagging the control to be redrawn when the MessagePump is run. You can do this by creating a .Net class that inherits from DataGridView and overrides the OnPaint() event. You will need access to a C# compiler to build this. Once built, you add it to the OpenSpan Studio Toolbox and replace your DataGridView controls with it.
(引用地址:http://wenku.baidu.com/link?url=uLLKAHMKVzub20zu--98KrBOsUnMB5qIa10CXaBjhq-AKHDLifA3V8j5gpM996vglpIq1-4aq26X7-rNJ-t_EbIEqtNtet0TT6rnVRYJh7G)
处理的方法,重新封装DataGridView 控件,重新OnPaint 方法,代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Windows.Forms;
namespace wstest
{
class DataGridViewForWs : DataGridView
{
protected override void OnPaint(PaintEventArgs e)
{
try
{
base.OnPaint(e);
}
catch
{
Invalidate();
}
}
}
}
使用封装的控件以后,经过多次测试,没有再出现大红叉!
老外的解释:
The DataGridView is a common .Net control used to display and permit editing of tabular data. It can be filled in via code or by attaching a data source to it.
DataGridViews, like most controls, are not thread-safe. That is, you need to perform operations on them using the same thread as they were created on. However, sometimes OpenSpan developers will accidently modify the DataGridView from another thread because the operation is coming from an event in another application.
A safe way to correct this problem and to ensure it doesn't accidently happen is to postpone updating the control during a OnPaint() event if it happens from another thread. This is done by handling an exception and then flagging the control to be redrawn when the MessagePump is run. You can do this by creating a .Net class that inherits from DataGridView and overrides the OnPaint() event. You will need access to a C# compiler to build this. Once built, you add it to the OpenSpan Studio Toolbox and replace your DataGridView controls with it.
(引用地址:http://wenku.baidu.com/link?url=uLLKAHMKVzub20zu--98KrBOsUnMB5qIa10CXaBjhq-AKHDLifA3V8j5gpM996vglpIq1-4aq26X7-rNJ-t_EbIEqtNtet0TT6rnVRYJh7G)
处理的方法,重新封装DataGridView 控件,重新OnPaint 方法,代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Windows.Forms;
namespace wstest
{
class DataGridViewForWs : DataGridView
{
protected override void OnPaint(PaintEventArgs e)
{
try
{
base.OnPaint(e);
}
catch
{
Invalidate();
}
}
}
}
使用封装的控件以后,经过多次测试,没有再出现大红叉!
0 0
- DataGridView 在多线程中使用可能出现大红叉,解决方法如下
- DataGridView出现大红叉--在使用多线程访问数据源时
- DataGridView绑定DataTable出现大红叉
- 在多线程中使用DataGridView的问题
- java中出现如下错误解决方法:
- 在Eclipse中配置SDK路径的时出现找不到adb.exe的提示,解决方法如下!
- java.util.HashMap在多线程环境中可能出现的问题
- C#中如何将dataGridView得滚动条拉到最下面,解决方法如下
- make时可能出现如下error
- 在windows下VC中编译多线程需要如下设置
- 在使用apt-get出现如下错误解决办法
- 导入excel到datagridview中出现未在本地计算机上注册“Mircosoft.Jet.OleDB.4.0”提供程序 解决方法
- VS2005 在线程中 使用 UPDATEDATA 出现错误 解决方法
- 在使用JasperReport中出现错误的部分解决方法
- 在IIS中使用pyodbc出现错误的解决方法
- 在node中使用GraphicsMagick出现以下错误的解决方法
- spring中使用hibernate出现的一个错误.错误信息如下:
- #include "winsock2.h" 解决c++程序中使用mysql错误 在用VC 连接Mysql 5.0 时出现如下错误
- 王宝强女儿妻子马蓉近照曝光 网友:这下可放心了!
- 虚拟机栈和本地方法栈溢出
- SSH应用--Hibernate 访问数据库的三种方法比较
- java基础__classLoader 类加载器
- ubuntu vi 配置
- DataGridView 在多线程中使用可能出现大红叉,解决方法如下
- Myeclipse插件实现Hibernate快速数据库访问的原理
- IOS 开发调用打电话,发短信
- JAVA NIO 简介
- FileSystemWatcher是软件程序开发常用到的一个监控组件,但是这个控件的Changed 时间方法老是触发2次或者多次,触发多次的情况
- 04.ServletContext读取资源文件
- 三分(初理解)。
- 【游戏课】技术片段之——弹簧质点模型与布料动画
- epoll 的 Edge Trigger 和 Level Trigger 理解