What is the easiest and fastest way to convert an array to BindingList?
Asked
Active
Viewed 6,914 times
4 Answers
10
Use the BindingList
constructor that takes an IList<T>
.
var binding = new BindingList<MyType>(myArray);

Oded
- 489,969
- 99
- 883
- 1,009
4
You're looking for the constructor:
var bl = new BindingList<YourClass>(arr);

SLaks
- 868,454
- 176
- 1,908
- 1,964
-
did not understand, can you give an example? – Okan Kocyigit Feb 27 '11 at 17:56
3
Be careful when using the BindingList(IList ..) constructor with an Array as the IList will be read-only.
Any attempts to add/remove from the BindingList will therefore throw a NotSupportedException as the IList can't handle the functionality as the Collection is read-only.
To create an editable BindingList you'll have to convert it to a list before using the IList constructor.
A nice description as to why Arrays are built from IList can be found here for some additional reading: Why array implements IList?

Community
- 1
- 1

David Moores
- 1,025
- 2
- 9
- 23
1
you can try a foreach cycle:
public void AppenFromArray(T[] aSource)
{
if (aSource == null) { return; }
foreach (T el in aSource)
{
this.Add(el);
}
}

Matteo TeoMan Mangano
- 430
- 4
- 14