ComboBox填充LINQ C#

时间:2009-03-14 22:00:16

标签: c# linq-to-sql

我有一个基于linq的windows项目。有一个表单可以保存personel的离开,标题,姓氏等。在这个表单中有2个组合框项目,包含标题和离开对象和1个文本框,1个列表框。加载我填充列表框与Personel Class。当列表框的选定索引更改事件发生时,我想用这个Personel的接班人和标题信息填充comboDepartman和comboTitle(从DB获取此信息)。我已经这样做但是有一个问题。当我这样做ComboBoxes只向我显示此人员的离职人员和标题信息不是整个部门和标题信息。当我填写Combobox与全部离职员和标题时,我想将selectedindex更改为personel的离职人员或职称。

有一个简单的解决方案吗?

//如果DepartmanID -1> selectedindex值比获取ArgumentOutOfRangeException所以我的算法不适用于这种情况。

  if (listPersonel.SelectedItem !=null)
        {
             Personel p = (Personel)listPersonel.SelectedItem;
             txtPersonelName.Text = p.PersonelName;
             txtPersonelNo.Text = p.PersonelNo.ToString();
             cmbDepartmant.SelectedIndex = Convert.ToInt32(p.DepartmantID) - 1;
             cmbTitle.SelectedIndex = Convert.ToInt32(p.TitleID) - 1;
        }

1 个答案:

答案 0 :(得分:1)

cmbDepartmant.SelectedValue = Convert.ToInt32(p.DepartmantID)