[英]C# ListBox : Items collection cannot be modified when the DataSource property is set
[英]no listbox items after assigning datasource c#
我一直在尝试用数据表填充列表框。
这是我所见过的几乎所有页面所建议的代码:
ListBox lb = new ListBox();
using (SqlCommand cmd = new SqlCommand("SELECT * FROM Files", con))
{
SqlDataAdapter dap = new SqlDataAdapter(cmd);
DataTable tblShapes = new DataTable();
dap.Fill(tblShapes);
lb.DataSource = tblShapes;
// define Display and Value members
lb.DisplayMember = "Name";
lb.ValueMember = "Id";
}
问题是,即使lb.datasource.rows
有我的记录, lb.Items
也没有项目,我也试图更改使用DisplayMember
和ValueMember
分配datasource
的顺序,但没有任何变化
C#Winforms
提前致谢
由于您是动态创建lsitbox的,因此需要添加到控件中,以后还要设置DataSource
ListBox lb = new ListBox();
this.Controls.Add(lb);
lb.DisplayMember = "Name";
lb.ValueMember = "Id";
lb.DataSource = tblShapes;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.