日期:2014-05-17  浏览次数:20752 次

winform里怎么把数据库的值赋给下拉框
下拉框的所有选项都会从数据库读取,数据库的表中变更后,下拉框里也会变,数据库的表里有很ID,下拉项的name等


新手求教……

------解决方案--------------------
SqlConnection con = new SqlConnection("Data Source = .;Initial Catalog=appdata;User ID=sa;Password= ;") ;
con.Open();
SqlDataAdapter sqlcmd = new SqlDataAdapter("select tj_code,code_name from appdata..tjkm where tj_lbmc = '住院收据'",con);
DataTable dt = new DataTable();
sqlcmd.Fill(dt);
con.Close();

cmb_nm.DisplayMember = "code_name";
cmb_nm.ValueMember = "tj_code";
cmb_nm.DataSource = dt;
cmb_nm.SelectedIndex = 0;
------解决方案--------------------
ComBoBox1.DisplayMember = "name";
ComBoBox1.ValueMember = "id";
ComBoBox1.DataSource = dt(查询出来的datatable);
ComBoBox1.DataBind();