0

使用 ADO.net,如果我想从组合框中检索 ID,我只需这样做:

int idToGet = int.parse(tbCategory.SelectedValue.ToString());

然后完成,

但是当我将组合框与 EF 绑定时,它会显示错误Input string was not in a correct format。所以当前值显示 { id = 7, category = TESTING } 而不是像往常一样。

这是我的代码片段:

    public void loadCategory()
    {
        tbCategory.DataSource = null;

        var listCategoryObj = new malsic_inventoryEntities();

        var query = from cat in listCategoryObj.Category
                    //join cat in listItmObj.Category on n.id_category equals cat.id
                    select new { cat.id,cat.category };

        if (query.Count() > 0)
        {
            tbCategory.DataSource = query.ToList();
            tbCategory.DisplayMember = "category";
            tbCategory.ValueMember = "id";
            tbCategory.Invalidate();
        }
        else
        {
            tbSubCategory.Enabled = false;
        }

    }
    public void loadSubcategory()
    {
        tbSubCategory.DataSource = null;

        int id_category_current = int.Parse(tbCategory.SelectedItem.Value.ToString());

        var listSubCategoryObj = new malsic_inventoryEntities();

        var query = from SubCat in listSubCategoryObj.SubCategories
                    where SubCat.id_category == id_category_current
                    select new { SubCat.id, SubCat.subcategory };

        if (query.Count() > 0)
        {
            groupBox1.Enabled = true;

            tbSubCategory.DataSource = query.ToList();
            tbSubCategory.DisplayMember = "subcategory";
            tbSubCategory.ValueMember = "id";
            tbSubCategory.Invalidate();
        }
        else
        {
            groupBox1.Enabled = false;
        }

    }

我做错了什么?

4

1 回答 1

0

我认为您的问题与 ADO.NET 或实体框架无关。我认为您的问题与int.Parse. 尝试id_category_current以这种方式设置,而不是现在的方式:

int id_category_current;
if(!int.TryParse(tbCategory.SelectedItem.Value.ToString(), out id_category_current))
{
   groupBox1.Enabled = false;
   return;
}
于 2013-10-24T16:44:19.893 回答