DataGridView not updating when additional rows are added to the bound DataSource unless I null it out first
I have a winForms DataGridView
bound to a List<MyObjectType>
. My problem is that once I do the initial myDataGridView.DataSource = myObjectList;
that adding/removing elements from the list 开发者_Python百科is not reflected in the displayed DataGridView even though in the debugger the row count on myDataGridView.DataSource
does update.
I'm able to force the DGV to update the displayed rows if I null the datasource before reassigning the list to it. This looks ugly though and I'm wondering if I didn't miss an easier way to do this.
myDataGridView.DataSource = null;
myDataGridView.DataSource = myObjectList;
The List<T>
collection does not support change notifications, so the DataGridView
will never be able to detect when you add/remove elements. Consider using ObservableCollection<T>
, which does support change notifications.
精彩评论