从BackgroundWorker更新DataGridView数据源时出错
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尽量不要在您还不需要的地方创建 DataTable。
我会删除这个:
并且只有当我可以用数据填充它时才会创建它。事实上,除非您想在许多不同的地方使用它,否则您绝对不需要它作为表单的一部分。
然后,我将其称为:
仅当工作线程完成所有负载时。
try not to create a DataTable where you do not need it yet.
I would remove this:
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:
only when all the load has been completed by the worker thread.
我遇到了与您相同的问题,只是我的 datagridview 绑定到了一个列表。当我说要清除列表以便我可以添加新值以使列表不会不正确时,我就出现了问题。我通过将数据源更改为其他内容来解决这个问题,做了我需要的操作,然后将其切换回来,但这不是最好的解决方法。
无论好坏,更改数据源是目前最安全的选择。然而,与其创建第二个数据表,不如:
这样您就不需要费心创建另一个数据表。
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:
That way you don't need to bother creating another datatable.