Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. LINQ
  4. 2 combos linked together - LINQ to Entities C# WinForms

2 combos linked together - LINQ to Entities C# WinForms

Scheduled Pinned Locked Moved LINQ
csharpwinformslinq
2 Posts 2 Posters 7 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Sami L
    wrote on last edited by
    #1

    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.

    J 1 Reply Last reply
    0
    • S Sami L

      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.

      J Offline
      J Offline
      JacquesDP
      wrote on last edited by
      #2

      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!

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups