当数据源改变时不会更新C#的DataGridView [英] C# DataGridView not updated when datasource is changed

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

问题描述

我有对象的列表

List<MobilePhone> results;



所以我说的列表中的datagridview

so i added the list to the datagridview

dataGridView.DataSource = phase3Results;



所以我有在下拉列表中所选项目的任何改变决定列表结果的一些下拉框箱,所以我的名单业绩的变化,但在DataGridView它没有体现出来。有没有什么办法来刷新的修改

so i have a few dropdown boxes which dictate the list results at any change of selected item in the dropdown boxes, so my list results changes, but on the datagridview its not reflected. is there any way to "refresh" the changes?

推荐答案

快速而肮脏的解决方案

dataGridView.DataSource = null;
dataGridView.DataSource = phase3Results;



清洁和正确的解决方案

使用的BindingList< T> 而不是清单< T> 为您的数据源。 列表< T> 不火的事件时,其收藏的变化

Use a BindingList<T> instead of List<T> as your DataSource. List<T> does not fire events when its collection changes.

另外,如果您还实施 INotifyPropertyChanged的 T 的BindingList< T> 自动订阅的属性更改每个 T 集合中,并且让视图知道这个更改。

Also, if you additionally implement INotifyPropertyChanged for T, BindingList<T> automatically subscribes to property changes for each T in the collection and lets the view know about the change.

这篇关于当数据源改变时不会更新C#的DataGridView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆