Thursday, May 21, 2009

Adding an empty item to an IList collection at runtime.

How can I add a new empty item to a datasource (BindingSource) if the collection that it contains has objects of unknown type at design time:

if (dataSource.GetType() == typeof(BindingSource))
{
IEnumerator en = dataSource.GetEnumerator();
en.MoveNext();
Type t = en.Current.GetType();

object o = System.Activator.CreateInstance(t, false);

dataSource.Add(o);
}


where dataSource is of type IList.

This was a request to add an empty line to a windows forms combobox and to a listbox.

BindingSource can be bound to any of the following:

  • Object
  • System.Type
  • IEnumerable
  • ICollection
  • IList
  • IListSource
  • IBindingList
  • IBindingListView

Labels:

0 Comments:

Post a Comment

<< Home