开发者

DataGridView not updating when additional rows are added to the bound DataSource unless I null it out first

开发者 https://www.devze.com 2023-01-14 22:32 出处:网络
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 开发者

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号