【问题标题】:ComboBox2 changes on ComboBox1 change - using LINQ EF Winform C#ComboBox2 更改 ComboBox1 更改 - 使用 LINQ EF Winform C#
【发布时间】:2012-03-28 22:41:39
【问题描述】:

我有 1 个带有 2 个组合框的 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第一行为空就好了。

【问题讨论】:

    标签: winforms linq c#-4.0 entity-framework-4 combobox


    【解决方案1】:

    您想要处理组合框1 的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 }); 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-11-18
      • 2020-01-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多