原链接:http://www.cnblogs.com/tiu/archive/2006/11/06/552142.html

 

这两天遇到ListBox数据绑定并显示的问题,以前以为可以根ASP.NET中的用法差不多,即

WinForm中ListBox数据绑定问题(转)ListBox listBox;
WinForm中ListBox数据绑定问题(转)          listBox.DataSource 
= ds;
WinForm中ListBox数据绑定问题(转)          listBox.DataTextField 
= "要显示的字段名";
WinForm中ListBox数据绑定问题(转)          listBox.DataValueField 
= "id";
WinForm中ListBox数据绑定问题(转)          listBox.DataBind();
然后利用listBox.SelectedItem即可访问被选中的项的值,当然在WinForm中除了DataSource的属性还有,其他都没有了,WinForm就换成如下方式:
WinForm中ListBox数据绑定问题(转)                listBox.DataSource = ds.Tables[0];
WinForm中ListBox数据绑定问题(转)                listBox.DisplayMember 
= "carsnumber";
WinForm中ListBox数据绑定问题(转)                listBox.ValueMember 
= "id";
这样便可在ListBox正确显示出来,并且利用listBox.SelectedValue可以得到选定项的对应的id,  但是当我用
listBox.SelectedItem打算得到相应的carsnumber值时,确显示System.Data.DataRowView,利用listBox.Item[]访问得到的结果是一样的。最后在网上搜搜看能不能找到答案,又在CSDN上搜了一下以前的帖子,最后找到了答案,
如果要循环访问绑定了的Text值和Value 值,可用如下方式:
WinForm中ListBox数据绑定问题(转)forint i = 0; i < listBox.Items.Count; i++ )
}
看来如果没有网络在学习的道路上还真不好走。。。

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-09-15
  • 2021-04-26
  • 2021-10-29
  • 2021-09-17
  • 2022-12-23
  • 2021-08-11
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-07-19
  • 2022-12-23
  • 2022-12-23
  • 2022-02-08
相关资源
相似解决方案