janderson
janderson

Reputation: 973

BindingList ListChanged event not firing until filled with data?

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.

Upvotes: 0

Views: 1487

Answers (1)

Phoenix_uy
Phoenix_uy

Reputation: 3294

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.

Upvotes: 2

Related Questions