问题
How can I cast an IList<Customer>
list to BindingList<Customer>
?
回答1:
var yourList = new List<Customer>();
var listBinding = new BindingList<Customer>(yourList);
BindingList Constructors
You don't need to do a cast, just provide the BindingList<T>
class constructor with IList<T>
, which you have.
回答2:
Unfortunately you can not cast an IList to something its not. However you can create a new BindingList from it fairly easy by just passing your IList into its constructor.
BindingList<Customer> bindingList = new BindingList<Customer>(yourIList);
回答3:
BindingList
constructor takes IList
parameter, use it:
var binding = new BindingList<Customer>(list); //where list is type of IList<Customer>
回答4:
IList<Customer> list = new List<Customer>();
var bindingList = new BindingList<Customer>(list);
回答5:
Additional information: IBindingList inherits from IList
: So IBindingList
shares all properties and function signatures with IList
. So, IList
implementations can readily "fit" IBindingList
implementations.
来源:https://stackoverflow.com/questions/14953461/convert-ilistt-to-bindinglistt