2017-06-06 14 views
0

私はcomboBoxContainerに問題があります。 comboBoxategorycomboBoxMarketが入力されていますが、comboBoxContainerは入力されていません。
selected.Containerには正しい変数がありますが、コンボボックスはこの変数を取得しません。C#Linq - コンボボックスが埋められていない

private void listBoxProducts_SelectedIndexChanged(object sender, EventArgs e) 
{ 
    try 
    { 
     ProductList_Variables selected = ProductList_Variables)listBoxProducts.SelectedItem; 

     textBoxProduct.Text = selected.Product; 
     comboBoxCategory.SelectedItem = selected.Category; 
     comboBoxMarket.SelectedItem = selected.Market; 
     comboBoxContainer.SelectedItem = selected.Container; 
     textBoxPrice1.Text = selected.Price.ToString(); 
    } 
    catch (Exception ex) 
    { 
     MessageBox.Show(ex.Message); 
    } 
} 
class ProductList_Variables 
{ 
    public int Id { get; set; } 
    public string Product { get; set; } 
    public string Category { get; set; } 
    public string Size { get; set; } 
    public string Market { get; set; } 
    public string ProductName { get { return Product + " - " + Category + " - Size: " + Size +", Market: "+ Market; } } 
    public string Flavour { get; set; } 
    public decimal Price { get; set; } 
    public string Container { get; set; } 
    public int IdContainer { get; set; } 


} 
void Fillcombo()// is filling the combobox 
{ 
    try 
    { 
     using (var db = new GelatoProjectDBEntities()) 
     { 
      var products = (from x in db.ProductsLists 
          select new ProductList_Variables { Id = x.Id, Product = x.Product, Category = x.Category, Size = x.Size, Market = x.Market, Container=x.Container, Price=x.Price, IdContainer=x.IdContainer } 
          ).OrderBy(c => c.Product).ToArray(); 
      listBoxProducts.Items.AddRange(products); 
      listBoxProducts.DisplayMember = "ProductName"; 
      listBoxProducts.ValueMember = "Id"; 
      var goods = (from x in db.Goods 
         select new ProductList_Variables { Id = x.Id, Product=x.item, Container = x.item} 
         ).OrderBy(c => c.Product).ToArray(); 

      comboBoxContainer.Items.AddRange(goods); 
      comboBoxContainer.DisplayMember = "Product"; 
      comboBoxContainer.ValueMember = "Id"; 
     } 
    } 
    catch (Exception ex) 
    { 
     MessageBox.Show(ex.Message); 
    } 
    } 
+0

'selected.Container'の型と値は何ですか?' comboBoxContainer'のデータは何ですか?単純な項目ではない場合( 'int' /' string'など)、どちらも同じソースから来ますか? – nvoigt

+0

'ProductList_Variables'の実装と、' comboBoxContainer'の記入方法を教えてください。 –

+0

@RolandBärとnvoigt、あなたのお問い合わせと更新された投稿 – Mirko

答えて

0

あなたのcomboBoxContainerはオブジェクトで満たされているようですが、文字列ではなくオブジェクトに一致する必要があります。

 comboBoxContainer.SelectedItem = 
      comboBoxContainer.Items.OfType<ProductList_Variables>().SingleOrDefault(x => x.Container == selected.Container); 
0

あなたの財産コンテナが値(comboBoxContainerのデータ要素のプロパティID)を含んでいる場合、あなたは

を使用することができますが、次のように

 comboBoxContainer.SelectedItem = selected.Container; 

あなたのラインを交換してみてください

comboBoxContainer.SelectedValue = selected.Container; 
関連する問題