更新数据源时刷新DataGridView

更新数据源时刷新DataGridView

本文介绍了更新数据源时刷新DataGridView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在更新基础数据源时刷新 DataGridView 的最佳方法是什么?

What is the best way to refresh a DataGridView when you update an underlying data source?

I' m经常更新数据源,并希望在结果发生时向用户显示结果。

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-24 07:47