本文共 2276 字,大约阅读时间需要 7 分钟。
?Windows????????????????DataSource????????DataSet?DataTable??????????????????????????????????
???????????????TextBox?????Label??????ListBox??????ComboBox????????DataGridView?????????????????????????????????????????????????????????????????
??????????????????????????
??????ComboBox??Windows?????????????????????????Windows????????????????????????????
??????????????????????????????DataSource?DisplayMember?ValueMember???????????
???????
ComboBox comboBox1 = new ComboBox(); SqlDataAdapter sda = new SqlDataAdapter("SELECT * FROM major", conn); DataSet ds = new DataSet(); sda.Fill(ds); comboBox1.DataSource = ds.Tables[0]; comboBox1.DisplayMember = "name"; comboBox1.ValueMember = "id"; ????T-SQL??????????
CREATE TABLE major ( id INT PRIMARY KEY IDENTITY(1,1), name VARCHAR(20) UNIQUE );
private void ComboBoxForm_Load(object sender, EventArgs e) { SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=test;User ID=sa;Password=root"); try { conn.Open(); SqlDataAdapter sda = new SqlDataAdapter("SELECT * FROM major", conn); DataSet ds = new DataSet(); sda.Fill(ds); comboBox1.DataSource = ds.Tables[0]; comboBox1.DisplayMember = "name"; comboBox1.ValueMember = "id"; } catch (Exception ex) { MessageBox.Show("?????" + ex.Message); } finally { if (conn != null) { conn.Close(); } } } private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) { if (comboBox1.Tag != null) { MessageBox.Show("????????" + comboBox1.Text); } } ???????????????????????????????????????????????
转载地址:http://atpz.baihongyu.com/