开发者

ComboBox and Entity Framework

I have ProjectID and Name in data table. cbProjects is ComboBox.

Here is a piece of code.

P开发者_如何学PythonrojectEntities projectE = new ProjectEntities();
cbProjects.ItemsSource = projectE.Projects;
cbProjects.DisplayMemberPath = "Name";

Can I get information about ProjectID of selected Name in ComboBox?


private void cbProjects_SelectedIndexChanged(object sender, System.EventArgs e)
{
    ComboBox cb = sender as ComboBox;

    var item = cb.SelectedItem as Project;
    int projectID = item.ProjectID;

}

or

var item = cbProjects.SelectedItem as Project;
int projectID = item.ProjectID;


I'm using VS 2010 with EF 4, here's the code which is working for me:

//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;


cbProjects.SelectedValuePath = "ProjectID"

Then you can get the id of selected item via cbProjects.SelectedValue.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜