如果选择组合框,如何在组合框中显示数据库值

时间:2014-04-13 03:16:21

标签: c#

如果选择组合框,如何在组合框中显示数据库值 我想根据数据库中的组合框项目类型(cboItemType)在组合框(cboItemName)中显示项目名称请帮忙。 。

2 个答案:

答案 0 :(得分:0)

粗略的例子:

cboItemType.DisplayMember = "Name"; // or property that you want to display
cboItemType.ValueMember = "ID"; // or property for the value

cboItemType.DataSource =
   database
   .ItemTypes
   .Where(someFilter)
   .ToList();

// to get selected value:

if (cboItemType.SelectedIndex == -1)
   return; // nothing selected

int selectedItemTypeID = (int)cboItemType.SelectedValue;
var selectedItemType =
   database
   .ItemTypes
   .Single(itemType => itemType.ID == selectedItemTypeID);

// do something with selectedItemType

答案 1 :(得分:0)

你可以试试这个

comboBox1.DataSource = ds.Tables[0];  // ds(DataSet Obj) 
comboBox1.DisplayMember = "cboItemName"; // column that to be displayed  
comboBox1.ValueMember = "cboItemType";  //  column name accourding to which value is displayed