我有數據表中的ProjectID和名稱。 cbProjects是ComboBox。組合框和實體框架
這是一段代碼。
ProjectEntities projectE = new ProjectEntities();
cbProjects.ItemsSource = projectE.Projects;
cbProjects.DisplayMemberPath = "Name";
我可以在ComboBox中獲得有關所選名稱的ProjectID的信息嗎?
我有數據表中的ProjectID和名稱。 cbProjects是ComboBox。組合框和實體框架
這是一段代碼。
ProjectEntities projectE = new ProjectEntities();
cbProjects.ItemsSource = projectE.Projects;
cbProjects.DisplayMemberPath = "Name";
我可以在ComboBox中獲得有關所選名稱的ProjectID的信息嗎?
cbProjects.SelectedValuePath = "ProjectID"
然後你就可以通過cbProjects.SelectedValue
獲得所選項目的ID。
private void cbProjects_SelectedIndexChanged(object sender, System.EventArgs e)
{
ComboBox cb = sender as ComboBox;
var item = cb.SelectedItem as Project;
int projectID = item.ProjectID;
}
或
var item = cbProjects.SelectedItem as Project;
int projectID = item.ProjectID;
我使用VS 2010年EF 4,這裏是這是爲我工作的代碼:
//load entities to the combo box
ProjectEntities projectE = new ProjectEntities();
var pdata = projectE.Projects;
cbProjects.DataSource = pdata; //cbProjects.ItemsSource
cbProjects.DisplayMember = "Name"; //cbProjects.DisplayMemberPath
//get selected value
var item = cbProjects.SelectedItem as Project;
int projectID = item.ProjectID;
Exaclty!需要從Combobox項目獲取實體對象。謝謝! – 2012-07-31 05:23:56