2012-03-28 9 views
0

私は1つのWinFormに2つのコンボボックスがあり、最初はEmployee名で満たされ、2番目のものはリストされたすべての従業員に影響を受けるタスク最初のコンボ。ComboBox1の変更はComboBox1の変更になります - LINQ EF Winform C#

  private void Form1_Load(object sender, EventArgs e) 
     { 
      using (LINQtoEntitiesEntities MyEntities = new LINQtoEntitiesEntities()) 
      { 
       ObjectQuery<Employee> Emp = MyEntities.Employee; 
       comboBox1.DataSource = (from u in Emp select new { u.ID, u.LastName }).ToList(); 
       comboBox1.ValueMember = "ID"; 
       comboBox1.DisplayMember = "LastName"; 
      } 
     } 

     private void comboBox1_TextChanged(object sender, EventArgs e) 
     { 
      if (comboBox1.SelectedIndex.ToString() == "0") return; 
      using (LINQtoEntitiesEntities MyEntities = new LINQtoEntitiesEntities()) 
      { 
       label1.Text = comboBox1.SelectedValue.ToString(); 
       ObjectQuery<Tasks> Tsk = MyEntities.Tasks; 
comboBox2.DataSource = (from t in Tsk where t.EmloyeeID.ToString() == comboBox1.SelectedValue.ToString() select new { t.ID, t.TaskName }).ToList(); 
       comboBox2.ValueMember = "ID"; 
       comboBox2.DisplayMember = "TaskName"; 
      } 
     } 

が正常にComboBox1のではなく、ComboBox2を記入でき、ComboBox1の最初の行が空白の場合、それは素晴らしいことだ。しかし、次のコードは、2番目のコンボのために実行することができませんでした。

答えて

0

TextChangedイベントの代わりに、combobox1のSelectedIndexChangedイベントを処理します。

空の値を挿入するには、これを試してみてください。

// replace the -1 with an appropriate value that matches the type of u.ID 
(from u in Emp select new { u.ID, u.LastName }).ToList().Insert(0, new { -1, string.Empty }); 
関連する問題