Is it possible to cast an IEnumerable list to a BindingList collection?
是否可以将IEnumerable列表转换为BindingList集合?
The IEnumerable list is a list of typed objects e.g:
IEnumerable列表是类型化对象的列表,例如:
IEnumerable<AccountInfo> accounts = bll.GetAccounts(u.UserName, u.Password);
And my PagingList just extends BindingList:
而我的PagingList只是扩展了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:
我只是想将我的IEnumerable列表传递给一个使用我的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
5 个解决方案
#1
BindingList<T>
implements IEnumerable<T>
, but not all IEnumerable<T>
are binding lists (in fact, most aren't).
BindingList
You should be able to create a new BindingList and add the items in the enumerable instance.
您应该能够创建一个新的BindingList并在可枚举实例中添加项目。
#2
Just to point out, your PagingList does not extend BindingList, it uses it through composition.
只是要指出,你的PagingList没有扩展BindingList,它通过组合使用它。
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'.
因此,您必须将您的AccountInfo项物理添加到BindingList实例属性'Collection'。
#3
You pass PagingList into your RenderListingsRows, which does not implement IEnumerable.
您将PagingList传递给RenderListingsRows,它不实现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.
通常,要使PagingList成为BindingList的扩展,它必须实现BindingList实现的所有接口。但目前它没有实现任何一个。
You should either inherit PagingList from BindingList, or implement all these interfaces, even if simply by calling methods of Collection object.
您应该从BindingList继承PagingList,或者实现所有这些接口,即使只是通过调用Collection对象的方法也是如此。
Or, you can just simply write for (var item in list.Collection)
或者,你可以简单地写一下(list.Collection中的var项)
#4
If your accounts collection implements IList<AccountInfo> you should be able to do this:
如果您的帐户集合实现IList
PagedList<AccountInfo> paged = new PagedList<AccountInfo>();
paged.Collection = new BindingList<AccountInfo>((IList<AccountInfo>)accounts);
#5
Send the binding list to the RenderListingsRows not the paging list. PagingList does not extend BindingList it uses composision instead. Hence the issue.
将绑定列表发送到RenderListingsRows而不是分页列表。 PagingList不会扩展BindingList,而是使用composision。因此这个问题。
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
}
}
#1
BindingList<T>
implements IEnumerable<T>
, but not all IEnumerable<T>
are binding lists (in fact, most aren't).
BindingList
You should be able to create a new BindingList and add the items in the enumerable instance.
您应该能够创建一个新的BindingList并在可枚举实例中添加项目。
#2
Just to point out, your PagingList does not extend BindingList, it uses it through composition.
只是要指出,你的PagingList没有扩展BindingList,它通过组合使用它。
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'.
因此,您必须将您的AccountInfo项物理添加到BindingList实例属性'Collection'。
#3
You pass PagingList into your RenderListingsRows, which does not implement IEnumerable.
您将PagingList传递给RenderListingsRows,它不实现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.
通常,要使PagingList成为BindingList的扩展,它必须实现BindingList实现的所有接口。但目前它没有实现任何一个。
You should either inherit PagingList from BindingList, or implement all these interfaces, even if simply by calling methods of Collection object.
您应该从BindingList继承PagingList,或者实现所有这些接口,即使只是通过调用Collection对象的方法也是如此。
Or, you can just simply write for (var item in list.Collection)
或者,你可以简单地写一下(list.Collection中的var项)
#4
If your accounts collection implements IList<AccountInfo> you should be able to do this:
如果您的帐户集合实现IList
PagedList<AccountInfo> paged = new PagedList<AccountInfo>();
paged.Collection = new BindingList<AccountInfo>((IList<AccountInfo>)accounts);
#5
Send the binding list to the RenderListingsRows not the paging list. PagingList does not extend BindingList it uses composision instead. Hence the issue.
将绑定列表发送到RenderListingsRows而不是分页列表。 PagingList不会扩展BindingList,而是使用composision。因此这个问题。
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
}
}