从另一个线程更新时,DataGridView 不会重新绘制自身
我在从另一个线程更新 DataGridView
时遇到问题。让我解释一下。当用户单击表单上的按钮时,我需要用一些行填充网格。这个过程需要一些时间,所以我在一个单独的线程中完成它。在启动线程之前,我将 DataGridView.Enabled 属性设置为 false,以防止用户在添加项目时对其进行编辑,并且在工作线程结束之前我设置了>启用返回到true
。
问题是如果需要显示滚动条,DataGridView
将无法正确更新其内容。我将用屏幕截图来说明这一点:
如您所见,最后一个可见行是部分绘制的,并且DataGridView
不会向下滚动。如果我调整网格大小,使其重新绘制自身,所有行都会正常显示。
这是一些代码:
private void button1_Click(object sender, EventArgs e)
{
string[] fileNames = new string[] { "file1", "file2", "file3" };
Thread AddFilesToListThread = new Thread(ThreadProcAddRowsToGrid);
dataGridView1.Enabled = false;
AddFilesToListThread.Start(fileNames);
}
delegate void EmptyDelegate();
private void ThreadProcAddRowsToGrid(object fileNames)
{
string[] files = (string[])fileNames;
foreach (string file in files)
{
EmptyDelegate func = delegate
{
dataGridView1.Rows.Add(file);
};
this.Invoke(func);
}
EmptyDelegate func1 = delegate
{
dataGridView1.Enabled = true;
};
this.BeginInvoke(func1);
}
我还注意到只有 Enabled
属性会导致这种奇怪的行为。例如,更改 BackgroundColor
效果很好。
你能帮我看看问题出在哪里吗?
I have a problem updating DataGridView
from another thread. Let me explain. When user clicks a button on a form I need to populate the grid with some rows. This process takes some time, so I'm doing it in a separate thread. Before starting the thread I set DataGridView.Enabled
property to false
, to prevent user from editing items while they are being added, and just before the working thread ends I set Enabled
back to true
.
The problem is DataGridView
won't update its contents correctly if scrollbars need to be shown. I'll illustrate this with a screenshot:
As you can see, the last visible row is partially drawn and the DataGridView
won't scroll down. If I resize the grid, making it to repaint itself, all rows appear normally.
Here is some code:
private void button1_Click(object sender, EventArgs e)
{
string[] fileNames = new string[] { "file1", "file2", "file3" };
Thread AddFilesToListThread = new Thread(ThreadProcAddRowsToGrid);
dataGridView1.Enabled = false;
AddFilesToListThread.Start(fileNames);
}
delegate void EmptyDelegate();
private void ThreadProcAddRowsToGrid(object fileNames)
{
string[] files = (string[])fileNames;
foreach (string file in files)
{
EmptyDelegate func = delegate
{
dataGridView1.Rows.Add(file);
};
this.Invoke(func);
}
EmptyDelegate func1 = delegate
{
dataGridView1.Enabled = true;
};
this.BeginInvoke(func1);
}
I've also notices that only Enabled
property causes this strange behaviour. Changing, for example, BackgroundColor
works fine.
Could you help me see where the problem is?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否尝试过
DataGridView.Refresh()
也许设置只读属性而不是 dataGridView1.Enabled = true;?
或者,我认为这可以通过将数据与用户界面分离来解决。
在我看来,这是一个简化的示例,但如果可以的话,我建议替换等效的行;
然后
您还可以使用 BindingSource 上的 ResetBindings 刷新数据;
Have you tried
DataGridView.Refresh()
Maybe setting the readonly property instead of dataGridView1.Enabled = true;?
Alternatively, I think this may be solved by separating your data from the UI.
It looks to me like this is a simplified example for SO here but if you can I would suggest replacing the equivalent line;
with
Then you can also refresh the data using ResetBindings on the BindingSource;