How can I cast an IList<Customer>
list to BindingList<Customer>
?
LukeHennerley
var yourList = new List<Customer>();
var listBinding = new BindingList<Customer>(yourList);
You don't need to do a cast, just provide the BindingList<T>
class constructor with IList<T>
, which you have.
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);
BindingList
constructor takes IList
parameter, use it:
var binding = new BindingList<Customer>(list); //where list is type of IList<Customer>
IList<Customer> list = new List<Customer>();
var bindingList = new BindingList<Customer>(list);
Michael Colorado
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