【发布时间】:2015-09-28 17:48:49
【问题描述】:
我在 Windows 窗体上有两个控件 - 一个组合框和一个列表框。
我正在尝试根据从 ComboBox 中选择的值填充 ListBox,但我不断收到以下错误。
无法将“System.Data.Entity.DynamicProxies.Category_B0496327038CB6B25A6EF7B750129DD7B44A87BE41C4E2DB0F8D7A00898B060F”类型的对象转换为“System.IConvertible”类型。
public partial class Form1 : Form
{
NorthWindEntities dbContext = new NorthWindEntities();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
cbx_Categories.DataSource = dbContext.Categories.ToList();
cbx_Categories.DisplayMember = "CategoryName";
cbx_Categories.ValueMember = "CategoryID";
}
private void cbx_Categories_SelectedIndexChanged(object sender, EventArgs e)
{
int categoryID = Convert.ToInt16(cbx_Categories.SelectedValue);
PopulateProductsLbx(categoryID);
}
void PopulateProductsLbx(int categoryID)
{
var products = from p in dbContext.Products
where p.CategoryID == categoryID
select p.ProductName;
lbx_Products.DataSource = products.ToList();
}
}
解决此问题的最佳方法是什么? 提前谢谢你。
【问题讨论】:
-
@AllanWoods "ComboBoxItem" 在 WinForms 中不可用
-
您可以尝试明确地说 cbx_Categories.SelectedValue.ToString()。
标签: c# winforms linq entity-framework