【发布时间】: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