c#datagridview红十字 [英] c# datagridview red cross

查看:191
本文介绍了c#datagridview红十字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个datagridview和datatable。我使用datatable作为datagridview的数据源。我使用线程添加和更新数据,如下所示。如果我完成了数据,我删除它。但是,在datagridview前面有两次大红x 。我找不到为什么?以下是我的示例。



注意:这并不总是发生,我只有两次这个错误,但是我需要处理!提前致谢。

 线程列表数据; 
DataTable dt =新的DataTable();
Form1_load()
{
dataGridview.DataSource = dt;
}

public void ListData()
{
foreach(数据中的var项目)
{
if(item.delete)
{
var row = dt.Rows.Find(item.id);
if(row!= null){row.Delete();}
continue;
}

listData = new Thread(delegate(){InsertOrUpdateData(item.Id);});
listData.Start(); listData.Join();
}

}

public void InserOrUpdateData(int id)
{
//这里我从数据库中检索一些数据
//并插入或更新到datatable
//像dt.Rows.Add(fields)和dt.Rows.Find(id)[fieldName] =new Value
}


解决方案

您需要使用 Invoke 方法

  if(gridView.InvokeRequired)
gridView.Invoke(new MethodInvoker(()=> gridView.DataSource = YOUR_DATASOURCE));
else
gridView.DataSource = YOUR_DATASOURCE;


I have a datagridview and datatable. I use the datatable as a datasource of the datagridview. I add and update the data using threads as below. And if I am done with the data I remove it. But two times there were big red x in front of the datagridview. I couldn't find out why? Below are my sample.

Note: This does not always occur, I got this error only two times but I need to handle! Thanks in advance.

  Thread listData;
  DataTable dt = new DataTable();
  Form1_load()
  {
    dataGridview.DataSource = dt;
  }

   public void ListData()
   {
      foreach(var item in data)
      {
          if(item.delete)
          { 
            var row = dt.Rows.Find(item.id); 
            if(row != null) { row.Delete();}
            continue;
          }

          listData = new Thread(delegate() { InsertOrUpdateData(item.Id); });
           listData.Start(); listData.Join();
       }

   }

      public void InserOrUpdateData(int id)
      {
         // Here I retrieve some data from database
         // and insert or update to the datatable
          // like dt.Rows.Add(fields) and dt.Rows.Find(id)["fieldName"] = "new Value"
      }

解决方案

You need to use Invoke method

if (gridView.InvokeRequired)
    gridView.Invoke(new MethodInvoker(() => gridView.DataSource = YOUR_DATASOURCE));
else
    gridView.DataSource = YOUR_DATASOURCE;

这篇关于c#datagridview红十字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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