IList和IList<>的困扰,编译不过

weyrewur 2014-08-27 11:28:49
我有下面几行很简单的代码,想把我自己初始化的一个IList<>赋值给bindingSource的List属性。这个属性的类型我看定义就是IList

public partial class Form1 : Form
{
private IList<string> m_List = new[] {"abc", "xyz"};
public Form1()
{
InitializeComponent();
this.bindingSource1.List = m_List;
}
}

编译错误有两个:
error CS0200: Property or indexer 'System.Windows.Forms.BindingSource.List' cannot be assigned to -- it is read only
error CS0266: Cannot implicitly convert type 'System.Collections.Generic.IList<string>' to 'System.Collections.IList'. An explicit conversion exists (are you missing a cast?)

我的代码应该如何修改呢?
...全文
199 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
於黾 2014-08-27
  • 打赏
  • 举报
回复
引用 6 楼 weyrewur 的回复:
[quote=引用 4 楼 xdashewan 的回复:] 首先bindingSource1.List是只读属性,不能用来set,其次你要绑定需要用bindingSource1.DataSource来完成绑定list
我能否通过属性窗口,来关联m_List和bindingSource.DataSource呢? 似乎在编辑属性的界面上找不到能绑定m_List的地方。难道只能手写代码绑定?[/quote] 为什么要这么干 既然数据本身写在后台,绑定的代码也写在后台不才是正常的思路么 你在后台定义个LIST,又跑回前台绑定??
xdashewan 2014-08-27
  • 打赏
  • 举报
回复
引用 6 楼 weyrewur 的回复:
我能否通过属性窗口,来关联m_List和bindingSource.DataSource呢? 似乎在编辑属性的界面上找不到能绑定m_List的地方。难道只能手写代码绑定?
嗯,这么说吧,虽然我不是很确定,但没有属性窗口可以用来绑定的可能性较大,原因是bindingSource绑定的对象多数是程序中的生成的对象,并非像dataset之类可以拖拽到界面上的对象,因此在设计视图里不会去扫描你在程序中所生成的对象,也就不会通过属性窗口让你绑定。 另外哪怕如gridview之类控件使用属性窗口绑定了dataset对象,表面上看是没在Form1.cs产生代码,但实际上只是把绑定对象的代码写在了Form1.Designer.cs,而两者的关系其实是partial class,也就是把Form1这个类的实现写在了不同文件里而已,所以你无需纠结代码绑定的问题。
weyrewur 2014-08-27
  • 打赏
  • 举报
回复
引用 4 楼 xdashewan 的回复:
首先bindingSource1.List是只读属性,不能用来set,其次你要绑定需要用bindingSource1.DataSource来完成绑定list
我能否通过属性窗口,来关联m_List和bindingSource.DataSource呢? 似乎在编辑属性的界面上找不到能绑定m_List的地方。难道只能手写代码绑定?
佛来寺 2014-08-27
  • 打赏
  • 举报
回复
System.Windows.Forms.BindingSource.List:只读 IList到List需要显示转换
xdashewan 2014-08-27
  • 打赏
  • 举报
回复
首先bindingSource1.List是只读属性,不能用来set,其次你要绑定需要用bindingSource1.DataSource来完成绑定list
於黾 2014-08-27
  • 打赏
  • 举报
回复
LIST继承自ILIST,但是它们毕竟不是一个类型 试试强制转换,如果不行,只能把你定义的ILIST改成LIST了
by_封爱 版主 2014-08-27
  • 打赏
  • 举报
回复
只读不能赋值的...
漫天雪飞 2014-08-27
  • 打赏
  • 举报
回复
第一,System.Windows.Forms.BindingSource.List 是只读的,不允许给赋值 第二,System.Collections.Generic.IList<string> 和System.Collections.IList 不是相同的类型,不能赋值

110,539

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧