开发者

how to get value member of combobox which is created using foreach loop

How can I get a valu开发者_运维问答e member of combobox which is created using foreach loop? It does not show the value member.

My code below:

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


You should rather do like this

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

If you want to display ID as display member than

 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
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜