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
精彩评论