2016-05-07 7 views
2

コンボボックスの値がテキストボックスに表示されます。 コンボボックスに "name"という属性が表示されているテーブルがあります。テキストボックスで選択されている値に基づいて、この値の属性 "価格"を導出する必要があります。データベースはADO.NETモデルを介して接続されています。 私は既に接続しているデータベース、すべてのものが動作し、すべてが保存され、変更されているので、CHANNEL型の "ConnectionString = @" Data Source = .... "は必要ではないと思います。私はテキストボックスに値を求めました。私はC#の初心者です。私は問題のためにたくさんのレッスンを見ましたが、私はこの接続文字列を使う必要はありません。 私はロシア語のGoogle翻訳者を英語に使っていました。 「誤解されているあなたのためにごめんなさい。コンボボックスの値は、テキストボックスに表示する必要があります

namespace test6 
    { 
     public partial class Form5 : Form 
     { 
     centrEntities db; 
     public Form5() 
    { 

     InitializeComponent(); 
     FillCombobox(); 

    } 

     private void Form5_Load(object sender, EventArgs e) 
    { 

     db = new centrEntities(); 
     db.Configuration.ProxyCreationEnabled = false; 
     db.Configuration.LazyLoadingEnabled = false; 
     orderBindingSource.DataSource = db.order.ToList(); 

    } 

    private void FillCombobox() 
    { 
     using (centrEntities c = new centrEntities()) 
     { 

      comboService.DataSource = c.service.ToList(); 
      comboService.ValueMember = "serviceID"; 
      comboService.DisplayMember = "name"; 


     } 
    } 

Table - values

how it looks.

答えて

1

私はあなたのコード内でイベントSelectedIndexChangedこのようなcomboServiceComboBoxに追加しています:

public partial class Form5 : Form 
{ 
    centrEntities db; 
    public Form5() 
    { 
     InitializeComponent(); 
     FillCombobox(); 
     comboService.SelectedIndexChanged += new EventHandler(comboService_SelectedIndexChanged); 
    } 

    private void Form5_Load(object sender, EventArgs e) 
    { 
     db = new centrEntities(); 
     db.Configuration.ProxyCreationEnabled = false; 
     db.Configuration.LazyLoadingEnabled = false; 
     orderBindingSource.DataSource = db.order.ToList(); 
    } 

    private void FillCombobox() 
    { 
     using (centrEntities c = new centrEntities()) 
     { 
      comboService.DataSource = c.service.ToList(); 
      comboService.ValueMember = "serviceID"; 
      comboService.DisplayMember = "name"; 
     } 
    } 

    private void comboService_SelectedIndexChanged(object sender, EventArgs e) 
    { 
     if (comboService.SelectedValue != null) 
     { 
      using (centrEntities c = new centrEntities()) 
      { 
       var price = (from serv in c.service 
          where serv.serviceID == Convert.ToInt32(comboService.SelectedValue) 
          select serv.price).SingleOrDefault(); 

       TextPriceName.Text = price.ToString(); 
      } 
     } 
    } 
} 
+0

おかげで男を。すごい仕事。真実のテキストボックスの表示は "価格"と "serviceID"ではありません。 –

+0

サービスを 'comboService.ValueMember =" price "'の価格で変更します。 –

+0

ああはい。私は気付かなかった。 しかし問題は半分で解決されます。実際、コンボボックスから値を選択すると、対応する値がテキストボックスに表示されます。ありがとうございます。しかし、次のテキストボックスへの移行、私が扱った最後の2つのフィールド(コンボボックス、テキストボックス)のデータは保存されず、空のままです。それは原則的にすべきです。 comboService.ValueMember = "serviceID"により、選択されたサービスがオーダーのあるテーブルに保持されます。この値はint値でテーブルに保存することはできませんが、 "price"を維持することを提案します。私はあなたの時間を取ることを申し訳ありません:) –

関連する問題