Okan Kocyigit
Okan Kocyigit

Reputation: 13421

how to convert array to BindingList

What is the easiest and fastest way to convert an array to BindingList?

Upvotes: 5

Views: 7097

Answers (4)

David Moores
David Moores

Reputation: 1145

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?

Upvotes: 3

Oded
Oded

Reputation: 499072

Use the BindingList constructor that takes an IList<T>.

var binding = new BindingList<MyType>(myArray);

Upvotes: 10

Matteo TeoMan Mangano
Matteo TeoMan Mangano

Reputation: 430

you can try a foreach cycle:

    public void AppenFromArray(T[] aSource)
    {
        if (aSource == null) { return; }

        foreach (T el in aSource)
        {
            this.Add(el);
        }
    }

Upvotes: 1

SLaks
SLaks

Reputation: 887489

You're looking for the constructor:

var bl = new BindingList<YourClass>(arr);

Upvotes: 4

Related Questions