【问题标题】:Entity Framework Winforms实体框架 Winforms
【发布时间】: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();
    }
}

解决此问题的最佳方法是什么? 提前谢谢你。

【问题讨论】:

标签: c# winforms linq entity-framework


【解决方案1】:

你有没有想过尝试

int categoryID = ((DataRowView)cbx_Categories.SelectedItem).Row["CategoryID"];

这应该会为你处理转换。

【讨论】:

  • 不好:无法将类型“object”隐式转换为“int”。存在显式转换(您是否缺少演员表?)
猜你喜欢
  • 2014-07-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-05
  • 2017-07-07
相关资源
最近更新 更多