-1

如何从与 linq 的组合框绑定中获取 valuemember

 cmb_projectName.DataSource = linq1.tbl_Projects.Select(c => new {c.ID,c.ProjectName }).ToList();

int projectID = Convert.ToInt32( cmb_projectName.SelectedValue);

cmb_projectName.DisplayMember = "项目名称"; cmb_projectName.ValueMember = "ID";


cmb_projectName.SelectedValue ==> return {ID = 1, ProjectName = "projectname1" }

我想返回 Just ID 值;

有什么问题 ??!!

4

2 回答 2

0

您必须ValueMember为您的 设置ComboBox,否则它将是您所看到的:

comboBox1.ValueMember = "ID";
//or
comboBox1.ValueMember = "ProjectName";//It's up to you

然后comboBox1.SelectedValue将返回IDProjectName取决于ValueMember您设置,假设您将其设置为"ID",因此您可以得到SelectedValue这样的:

var id = (int) comboBox1.SelectedValue;
于 2013-12-07T07:54:53.570 回答
0

您的情况下的错误代码:

cbo.DataSource = StronglyDataTable.OrderBy(x => x.pName)).Select(x => new { x.pID, x.pName });
cbo.DisplayMember = "pName";
cbo.ValueMember = "pID";

正确的代码:

cbo.DisplayMember = "pName";
cbo.ValueMember = "pID";
cbo.DataSource = StronglyDataTable.OrderBy(x => x.pName)).Select(x => new { x.pID, x.pName });
于 2019-02-26T03:32:06.293 回答