赋值:
第一种方法:
控件名称为:cmbXb
cmbXb.Items.Add("男");
cmbXb.Items.Add("女");
cmbXb.SelectedIndex = 0;
第二种方法:
控件名称为:cbxGuoji
string sql = "select id,mc from ZDYLX where fl = 'gj' and zt = '正常'";
List<NationalityInfo> Mylist = new List<NationalityInfo>();
NationalityInfo nationality = null;
nationality = new NationalityInfo();
nationality.id = Convert.ToInt32(0);
nationality.mc = "";
Mylist.Add(nationality);
using (DataSet Myds = DbHelperSQL.Query(sql))
{
using (DataTable Mytable = Myds.Tables[0])
{
for (int i = 0; i < Mytable.Rows.Count; i++)
{
nationality = new NationalityInfo();
nationality.id = Convert.ToInt32(Mytable.Rows[i][0].ToString());
nationality.mc = Mytable.Rows[i][1].ToString();
Mylist.Add(nationality);
}
}
}
//清除原有数据
cbxGuoji.DataSource = null;
//绑定新数据
cbxGuoji.DataSource = Mylist;
cbxGuoji.DisplayMember = "mc";
cbxGuoji.ValueMember = "id";
使用:
第一种方法: cbxGuoji
string nation = Convert.ToString(myTable.Rows[0][11]);
cbxGuoji.Text = nation;
第二种方法: cbxMoney
string npjsr = Convert.ToString(myTable.Rows[0][23]);
for (int i = 0; i < cbxMoney.Items.Count; i++)
{
if (cbxMoney.GetItemText(cbxMoney.Items[i]) == npjsr)
{
cbxMoney.SelectedItem = cbxMoney.Items[i];
}
}