Do I need a BindingSource AND a BindingList for WinForms DataBinding?

前端 未结 2 623
孤街浪徒
孤街浪徒 2020-12-14 22:35

I want to display a list of people in a DataGridView in a Windows Forms app. I want my service layer to return a list of Person objects (e.g., IList

2条回答
  •  暖寄归人
    2020-12-14 22:55

    Binding to an IList will only give you one-way binding; changes to the list or list items will not be reflected in the DataGridView. You can use a BindingList or BindingSource to get this functionality instead, but your Person class will still need to support INotifyPropertyChanged or else you will only get synchronisation when items are added/removed to/from the list, not when the list items themselves change.

    If you want to avoid a dependency on System.Windows.Forms, you could use ObservableCollection instead; this supports the necessary change notifications and can therefore be used as a two-way binding source.

提交回复
热议问题