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

5

6

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

Subatomic answered 1/7, 2009 at 16:28 Comment(1)
Remember, a cast is often just a check to see if the object you're referring to really is the type you're saying it is. In this case it is not an attempt to somehow morph a different object into some shape that it does not already have. If that is not clear, this article might help: blogs.msdn.com/ericlippert/archive/2009/03/19/…Bookstall
R
4

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'.

Reset answered 8/11, 2016 at 11:14 Comment(0)
F
3

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.

Forcible answered 1/7, 2009 at 16:31 Comment(0)
B
1

You pass PagingList into your RenderListingsRows, which does not implement IEnumerable.

In general, for PagingList to be an extension over BindingList it has to implement all interfaces that BindingList implements. But currently it does not implement any of them.

You should either inherit PagingList from BindingList, or implement all these interfaces, even if simply by calling methods of Collection object.

Or, you can just simply write for (var item in list.Collection)

Bugbane answered 1/7, 2009 at 16:34 Comment(0)
V
0

If your accounts collection implements IList<AccountInfo> you should be able to do this:

PagedList<AccountInfo> paged = new PagedList<AccountInfo>();
paged.Collection = new BindingList<AccountInfo>((IList<AccountInfo>)accounts);
Venerable answered 1/7, 2009 at 16:39 Comment(0)
P
0

Send the binding list to the RenderListingsRows not the paging list. PagingList does not extend BindingList it uses composision instead. Hence the issue.

Example below.

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

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

        }

        public void CallRenderListingsRows()
        {
            var pagingList = new PagingList<PostcodeDetail>();

            RenderListingsRows(pagingList.Collection);
        }

        protected void RenderListingsRows(BindingList<PostcodeDetail> list)
        {
            foreach (var item in list)
            {
                //render stuff
            }
        }
Pedicle answered 2/7, 2009 at 11:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.