如何获得使用foreach循环创建的组合框的值成员

时间:2011-04-20 07:10:12

标签: c#

如何获得使用foreach循环创建的组合框的值成员?它不显示值成员。

我的代码如下:

DataSet dsLoadWorkHourId = new DataSet();
dsLoadWorkHourId = Workhour.LoadWorkId();
foreach (DataRow row in dsLoadWorkHourId.Tables[0].Rows)
{
    cmbWorkHourId.Items.Add(row["SHIFT"].ToString());
}

1 个答案:

答案 0 :(得分:2)

你应该这样做

 DataSet dsLoadWorkHourId = new DataSet();
 dsLoadWorkHourId = Workhour.LoadWorkId();
cmbWorkHourID.DataSource = dsLoadWorkHourId.Tables[0]
cmbWorkHourID.DisplayMember = "SHIFT";
cmbWorkHourID.ValueMember = "SHIFTID"; //Use the ID column name here

如果要将ID显示为显示成员而不是

 DataSet dsLoadWorkHourId = new DataSet();
 dsLoadWorkHourId = Workhour.LoadWorkId();
cmbWorkHourID.DataSource = dsLoadWorkHourId.Tables[0]
cmbWorkHourID.DisplayMember = "SHIFTID"; //use the ID column name here
cmbWorkHourID.ValueMember = "SHIFTID"; //Use the ID column name here