从BackgroundWorker更新DataGridView数据源时出错

发布于 2024-12-04 01:21:59 字数 1206 浏览 1 评论 0原文

我有一个 DataGridView,它使用 DataTable 作为其数据源。我还有一个用于更新 DataTable 对象的 BackgroundWorker。当 DataTable 对象更新时,这会导致 IndexOutOfRange 异常。如果我在主 UI 线程上更新 DataTable,也不例外。

我怎样才能防止这种异常?我只是更新数据表,而不是重新分配数据源。

public partial class Form1 : Form
{
     DataTable myData = null;
     BackgroundWorker worker = new BackgroundWorker();

     public Form1()
    {
        InitializeComponent();
        myData = new DataTable();         
    }

     private void Form1_Load(object sender, EventArgs e)
    {
        dataGridView1.DataSource = myData;
    }

     private void button1_Click(object sender, EventArgs e)
    {
         worker.DoWork += new DoWorkEventHandler(PopulateData);
         worker.RunWorkerAsync();
    }

     private void PopulateData(object sender, DoWorkEventArgs e)
    {
          ......
          //update datatable
          myDataAdapter.Fill(myData); //Exception caused by this call
     }
}

编辑:我可以通过创建第二个 DataTable 对象并更新它,然后在 BackgroundWorker RunWorkerCompleted 事件中设置 Datasource DataTable = 更新的 DataTable 来解决此问题,但必须拥有两个 DataTable 似乎有点愚蠢并且浪费资源。

编辑:异常是

Application.Run(new Form1());

在 Program.cs 中引发的,即不是由我的代码引发的。

I have a DataGridView that uses a DataTable for its Datasource. I also have a BackgroundWorker that updates the DataTable object. This caueses an IndexOutOfRange exception when the DataTable object is updated. If I updated the DataTable on the main UI thread, there is no exception.

How can I prevent this exception? I am only updating the DataTable, not re-assigning the Datasource.

public partial class Form1 : Form
{
     DataTable myData = null;
     BackgroundWorker worker = new BackgroundWorker();

     public Form1()
    {
        InitializeComponent();
        myData = new DataTable();         
    }

     private void Form1_Load(object sender, EventArgs e)
    {
        dataGridView1.DataSource = myData;
    }

     private void button1_Click(object sender, EventArgs e)
    {
         worker.DoWork += new DoWorkEventHandler(PopulateData);
         worker.RunWorkerAsync();
    }

     private void PopulateData(object sender, DoWorkEventArgs e)
    {
          ......
          //update datatable
          myDataAdapter.Fill(myData); //Exception caused by this call
     }
}

Edit: I can workaround this by creating a second DataTable object and updating that, then setting the Datasource DataTable = the updated DataTable in the BackgroundWorker RunWorkerCompleted event, but having to have two DataTables seems a bit silly and a waste of resources.

Edit: The exception is thrown at

Application.Run(new Form1());

in Program.cs, i.e., not by my code.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

感情洁癖 2024-12-11 01:21:59

尽量不要在您还不需要的地方创建 DataTable。

我会删除这个:

myData = new DataTable();

并且只有当我可以用数据填充它时才会创建它。事实上,除非您想在许多不同的地方使用它,否则您绝对不需要它作为表单的一部分。

然后,我将其称为:

dataGridView1.DataSource = myData;

仅当工作线程完成所有负载时。

try not to create a DataTable where you do not need it yet.

I would remove this:

myData = new DataTable();

and would create it only when I can fill it with data. in fact you don't absolutely need it as part of the Form unless you want to use it from many different places.

I would then call this one:

dataGridView1.DataSource = myData;

only when all the load has been completed by the worker thread.

仙气飘飘 2024-12-11 01:21:59

我遇到了与您相同的问题,只是我的 datagridview 绑定到了一个列表。当我说要清除列表以便我可以添加新值以使列表不会不正确时,我就出现了问题。我通过将数据源更改为其他内容来解决这个问题,做了我需要的操作,然后将其切换回来,但这不是最好的解决方法。

无论好坏,更改数据源是目前最安全的选择。然而,与其创建第二个数据表,不如:

dataGridView1.DataSource = null;

这样您就不需要费心创建另一个数据表。

I had the same problem you were having except my datagridview was bound to a list. The problem occured for me when I said for the list to clear so I could then add in the new values so the list wouldn't be incorrect. I solved it by changing the datasource to something else, did what I needed to then switched it back, however it's not the nicest of work arounds.

Regardless if it's nice or not, changing the datasource is the safest bet at the moment. However instead of creating a second datatable it would be much better to:

dataGridView1.DataSource = null;

That way you don't need to bother creating another datatable.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文