Samio

Samio

  • NA
  • 201
  • 176.7k

Initialize Combo box with a blank item - LINQ C#

Mar 30 2012 10:35 AM
I would like to initialize my combo box with a set of items, the first item should be blank, and this is my code:

private void Form1_Load(object sender, EventArgs e)
{
using (LINQtoEntitiesEntities MyEntities = new LINQtoEntitiesEntities())
{
ObjectQuery<Employee> Emp = MyEntities.Employee;
//comboBox1.Items.Insert(0, String.Empty);
comboBox1.DataSource = (from u in Emp select new { u.ID, u.LastName }).ToList();
comboBox1.ValueMember = "ID";
comboBox1.DisplayMember = "LastName";
label1.Text = comboBox1.SelectedValue.ToString();
}
}


private void comboBox1_TextChanged(object sender, EventArgs e)
{
if (comboBox1.SelectedIndex.ToString() == "0") return;
using (LINQtoEntitiesEntities MyEntities = new LINQtoEntitiesEntities())
{
ObjectQuery<TasksPerEmployee> Tsk = MyEntities.TasksPerEmployee;
label1.Text = comboBox1.SelectedValue.ToString();
int val = Convert.ToInt32(comboBox1.SelectedValue.ToString());
comboBox2.DataSource = (from t in Tsk where t.EmployeeID == val select new { t.ID, t.TaskName }).ToList();
comboBox2.ValueMember = "ID";
comboBox2.DisplayMember = "TaskName";
}
}

Answers (1)