更新数据源时刷新 DataGridView [英] Refresh DataGridView when updating data source
问题描述
更新基础数据源时刷新 DataGridView
的最佳方法是什么?
What is the best way to refresh a DataGridView
when you update an underlying data source?
我经常更新数据源,并希望在结果发生时向用户显示.
I'm updating the datasource frequently and wanted to display the outcome to the user as it happens.
我有这样的东西(它可以工作),但是将 DataGridView.DataSource
设置为 null
似乎不是正确的方法.
I've got something like this (and it works), but setting the DataGridView.DataSource
to null
doesn't seem like the right way.
List<ItemState> itemStates = new List<ItemState>();
dataGridView1.DataSource = itemStates;
for (int i = 0; i < 10; i++) {
itemStates.Add(new ItemState { Id = i.ToString() });
dataGridView1.DataSource = null;
dataGridView1.DataSource = itemStates;
System.Threading.Thread.Sleep(500);
}
推荐答案
嗯,没有比这更好的了.正式地,你应该使用
Well, it doesn't get much better than that. Officially, you should use
dataGridView1.DataSource = typeof(List);
dataGridView1.DataSource = itemStates;
它仍然是一种清除/重置源"的解决方案,但我还没有找到任何其他可以可靠地刷新 DGV 数据源的方法.
It's still a "clear/reset source" kind of solution, but I have yet to find anything else that would reliably refresh the DGV data source.
这篇关于更新数据源时刷新 DataGridView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!