Why can i not cast an IEnumerable<T> list to a BindingList<t>?

Ben Dykes picture Ben Dykes · Jul 1, 2009 · Viewed 8.2k times · Source

Is it possible to cast an IEnumerable list to a BindingList collection?

The IEnumerable list is a list of typed objects e.g:

IEnumerable<AccountInfo> accounts = bll.GetAccounts(u.UserName, u.Password);

And my PagingList just extends BindingList:

public class PagingList<T> 
{
    public BindingList<T> Collection { get; set; }
    public int Count { get; set; }

    public PagingList()
    {
        Collection = new BindingList<T>();
        Count = 0;
    }
}

I just wanted to pass my IEnumerable list to a method that renders out the list with my PagingControl:

 protected void RenderListingsRows(PagingList<AccountInfo> list)
   {
     foreach (var item in list)
     {
       //render stuff
     }
   }

But it seems i cannot cast between the two, can anyone point out what i'm missing?!

Many thanks

Ben

Answer

Lucero picture Lucero · Jul 1, 2009

BindingList<T> implements IEnumerable<T>, but not all IEnumerable<T> are binding lists (in fact, most aren't).

You should be able to create a new BindingList and add the items in the enumerable instance.