i have 2 controls on windows form - combobox , listbox.
i'm trying populate listbox based on selected value combobox, keep getting following error.
unable cast object of type 'system.data.entity.dynamicproxies.category_b0496327038cb6b25a6ef7b750129dd7b44a87be41c4e2db0f8d7a00898b060f' type '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 = p in dbcontext.products p.categoryid == categoryid select p.productname; lbx_products.datasource = products.tolist(); } } what best approach resolve this? thank in advance.
have thought trying
int categoryid = ((datarowview)cbx_categories.selecteditem).row["categoryid"];
this should handle casting you.
Comments
Post a Comment