开发者

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

开发者 https://www.devze.com 2023-02-28 01:33 出处:网络
How can I get a valu开发者_运维问答e member of combobox which is created using foreach loop? It does not show the value member.

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

精彩评论

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