0

I am trying to databind a database table with two columns to a combobox, can this be done with linq2sql/dbml? Seems like it should be possible, but I can't find an object property to assign as the DataMember, even trying the reflection tricks mentioned in other posts.

var itemslist = from items in dbcontext.itemslisttable select items;
comboBox1.DataSource = itemslist;
comboBox1.DisplayMember = ???

something like

comboBox1.DisplayMember = itemslist.ItemDescriptionColumn.Name;

or

comboBox1.DisplayMember = itemslist.First().ItemDescriptionColumn.Name;

am I way off here, should I just use a string literal and be done with it?

4

1 回答 1

0

是的,您应该能够使用 LINQtoSQL/dbl 上下文类作为组合框的数据源。

关于组合框的一些信息:

  • Combobox的DisplayMember获取或设置一个字符串,该字符串指定要显示其内容的数据源的属性。
  • ValueMember属性也获取或设置一个字符串;它确定将哪个值移动到组合框的 SelectedValue 中。
var itemslist = from items in dbContext.itemslisttable
            select items;

comboBox1.DataSource = itemslist;
comboBox1.DisplayMember = "Name"; 
comboBox1.ValueMember = "ProductID"; 
于 2013-10-23T05:14:21.977 回答