将IList <T>转换为BindingList <T>

Mas*_*oud 17 c# generics casting list bindinglist

如何将IList<Customer>列表转换为BindingList<Customer>

Luk*_*ley 71

var yourList = new List<Customer>();
var listBinding = new BindingList<Customer>(yourList);
Run Code Online (Sandbox Code Playgroud)

BindingList构造函数

您不需要进行强制转换,只需提供BindingList<T>类构造函数即可IList<T>.


Eve*_*lie 9

不幸的是,你无法将IList强加给它.但是,只需将IList传递给构造函数,就可以相当容易地创建一个新的BindingList.

BindingList<Customer> bindingList = new BindingList<Customer>(yourIList);
Run Code Online (Sandbox Code Playgroud)

  • 已经有3个答案说同样的事情,我不明白为什么还需要更多:P (3认同)

gza*_*axx 6

BindingList构造函数接受IList参数,使用它:

var binding = new BindingList<Customer>(list); //where list is type of IList<Customer>
Run Code Online (Sandbox Code Playgroud)


Mit*_*eat 5

        IList<Customer> list = new List<Customer>();

        var bindingList = new BindingList<Customer>(list);
Run Code Online (Sandbox Code Playgroud)