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

bindinglist, c#, ienumerable

Solution

Just to point out, your PagingList does not extend BindingList, it uses it through composition.

I came across this looking for a similar answer. None of the answers here seem to provide a clear solution to your question, although they mentioned valuable points in figuring it out. I thought I'd add one for anyone passing by.

So given the information provided, the simple answer is no, but a simple solution to what you need without refactoring your classes is this:

IEnumerable<AccountInfo> accounts= bll.GetAccounts(u.UserName, u.Password);
myPagingList.Collection = new BindingList<Foo>(myfoos.ToList());

So you'll have to physically add your AccountInfo items to your BindingList instance property 'Collection'.

Problem

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

Original source