2 combos linked together - LINQ to Entities C# WinForms
-
I have 1 WinForm with 2 Combo boxes, first one is filled with Employee names, and the second one is supposed to be filled with tasks that are affected to every employee listed in the first combo. But could not get the following code to run for the second combo: private void Form1_Load(object sender, EventArgs e) { using (LINQtoEntitiesEntities MyEntities = new LINQtoEntitiesEntities()) { ObjectQuery 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 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"; } } Could fill normally ComboBox1, but not ComboBox2, and it would be great if first line of ComboBox1 is blank.
-
I have 1 WinForm with 2 Combo boxes, first one is filled with Employee names, and the second one is supposed to be filled with tasks that are affected to every employee listed in the first combo. But could not get the following code to run for the second combo: private void Form1_Load(object sender, EventArgs e) { using (LINQtoEntitiesEntities MyEntities = new LINQtoEntitiesEntities()) { ObjectQuery 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 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"; } } Could fill normally ComboBox1, but not ComboBox2, and it would be great if first line of ComboBox1 is blank.
Does the event fire for comboBox1_TextChanged, if not rather try the SelectedIndexChanged event. If EmployeeID is an int, rather convert to toInt32 than toString, reason is that "1" != "1 ", so it is better practice to compare int values.
Sami L wrote:
(from t in Tsk where t.EmloyeeID.ToString() == comboBox1.SelectedValue.ToString()
int selectedID;
if(int.TryParse(comboBox1.SelectedValue.ToString(), out selectedID))
{
comboBox2.DataSource = (from t in Tsk
where t.EmloyeeID == selectedID
select new { t.ID, t.TaskName }).ToList();
}To insert an empty value first in comboBox1 use the insert command after setting the datasource in the form load. the index will be 0 to insert as the first item.
comboBox1.Items.Insert(index, object);
No matter how long he who laughs last laughs, he who laughs first has a head start!