Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Common bindable interface

What it makes that collections are bindable? Is there common interface for List, DataTable with can bind to same control?

like image 752
userbb Avatar asked Nov 20 '25 22:11

userbb


1 Answers

To explain how List<T> and DataTable work (the footnote of the question), read the following but noting that:

  • List<T> implements IList and has a public T this[int index] {get;} which is used for resolving metadata
  • DataTable implements IListSource, which provides the table's default DataView; the DataView implements IList, and implements ITypedList to provide metadata

Collections bind in the following order:

  • the source is tested for IListSource; if available the IList is obtained via GetList()
  • otherwise the source is tested for IList; if unavailable an exception is thrown

then metadata for the IList is queried:

  • the IList is tested for ITypedList; if available this is used via GetProperties
  • the IList is tested for a public typed (non-object) indexer, i.e. public Foo this[int index] { get; } - if found, Foo is implied as the type and metadata obtained via TypeDescriptor.GetProperties(Type)
  • else the first item (if non-empty) is queried via GetType() for the type, and metadata obtained via TypeDescriptor.GetProperties(Type)

we now have access to the items (IList) and their metadata; additional support (optional) is provided via IBindingList (provides 2-way binding and basic sorting etc), IBindingListView (provides advanced sorting, filtering, etc), ICancelAddNew and IRaiseItemChangedEvents.

For most common scenarios (show data and push changes back) List<T> is fine; if you need to show unrelated updates as they happen BindingList<T> helps - but note that to support member level updates (as opposed to just add/remove/etc) the T must implement INotifyPropertyChanged

For reference, "metadata" here means "a set of PropertyDescriptors" (1 per column/property), which provide access to the underlying data (when supplied with an object), and information about the member itself (name, type, etc).

like image 103
Marc Gravell Avatar answered Nov 23 '25 13:11

Marc Gravell



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!