的BindingList ListChanged事件不触发,直到填满数据? [英] BindingList ListChanged event not firing until filled with data?
问题描述
我在写code,最终从远程数据库提取数据到使用数据绑定一个DataGridView。我目前正在创建code使用CSV文件和的BindingList作为测试工作。
I'm writing code to eventually pull data from a remote database into a DataGridView using a databinding. I am currently creating the code to work with a CSV file and a BindingList as a test.
我希望我的窗体上的显示屏,显示最后一次数据库更新。我目前使用我的BindingList的ListChanged事件来更新最后数据库更新显示。
I want a display on my form that shows the last time the database was updated. I'm currently using the ListChanged event on my BindingList to update the "last database update" display.
在ListChanged事件似乎如果它钩住了最初填充数据库之后才能发射。下面是从我班的一些code扩展DataGridView的:
The ListChanged event seems to only be firing if it's hooked up after the database is initially populated. Here's some code from my class that extends DataGridView:
BindingList<CsvTest> Data = new BindingList<CsvTest>;
public void InitGrid()
{
// Data.ListChanged += Data_ListChanged; // Event never fires if this is here!
Data = CsvTest.ParseCsv("test.csv");
Data.ListChanged += Data_ListChanged; // Working when it's here!
this.DataSource = Data; // DataGridView DataSource
}
我想为我的延迟更新为最初填充列表。任何人都可以想到的任何理由,这是不工作?
I would like for my delay to update as the list is initially populated. Can anyone think of any reason why this isn't working?
非常感谢。
推荐答案
行
Data = CsvTest.ParseCsv("test.csv");
将覆盖你的内容数据
变量。那是以前设置的任何值(例如 Data.ListChanged
)将属于旧的BindingList
对象。而新的的BindingList
对象不具有价值,直到你把它。
will overwrite the content of you Data
variable. Any value that was set before (for example Data.ListChanged
) will belong to the old BindingList
object. And your new BindingList
object does not have the value, until you set it.
如果您想设置之前 ParseCsv
的价值,你将不得不清除的BindingList
在数据
,然后从 ParseCsv
。
If you would like to set the value before ParseCsv
, you will have to clear the BindingList
in Data
and then add all items from ParseCsv
.
这篇关于的BindingList ListChanged事件不触发,直到填满数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!