我有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的第一行是空白的话会很棒。
答案 0 :(得分:0)
您想要处理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 });