BindingList ListChanged event not firing until filled with data?

烂漫一生 提交于 2019-12-13 04:23:50

问题


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.

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.

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?

Thanks a lot.


回答1:


The line

Data = CsvTest.ParseCsv("test.csv");

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.

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.



来源:https://stackoverflow.com/questions/25966963/bindinglist-listchanged-event-not-firing-until-filled-with-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!