Winforms ComboBox1更改上的ComboBox2更改-使用LINQ EF Winform C

Winforms ComboBox1更改上的ComboBox2更改-使用LINQ EF Winform C,winforms,linq,c#-4.0,entity-framework-4,combobox,Winforms,Linq,C# 4.0,Entity Framework 4,Combobox,我有一个WinForm和两个组合框,第一个框填充员工姓名,第二个框应该填充对第一个组合框中列出的每个员工都有影响的任务。但无法为第二个组合运行以下代码: private void Form1_Load(object sender, EventArgs e) { using (LINQtoEntitiesEntities MyEntities = new LINQtoEntitiesEntities()) {

我有一个WinForm和两个组合框,第一个框填充员工姓名,第二个框应该填充对第一个组合框中列出的每个员工都有影响的任务。但无法为第二个组合运行以下代码:

            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的第一行为空就更好了。

您希望处理ComboBox1的SelectedIndexChanged事件,而不是TextChanged事件

要插入空值,请尝试以下操作:

// 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 });