1 string sql = "select distinct RoleName from tb_Role"; 2 DataTable dt = SqlHelper.DataTable(sql); 3 cmb_Authority.Items.Clear();//移除下拉框中所有集合 4 foreach (DataRow dr in dt.Rows)//遍歷數(shù)據(jù)表中行的數(shù)據(jù) 5 { 6 cmb_Authority.Items.Add(dr[0]);//把表中第一列的數(shù)據(jù)添加到下拉框中 7 } 8 if (xiu==0) 9 { 10 cmb_Authority.SelectedIndex = 0; 11 } 12 if (xiu==1) 13 { 14 btn_Reg.Text = "修改"; 15 txt_OperatorName.Text = tb_users.OperatorName; 16 txt_UserName.Text = tb_users.UserName; 17 txt_UserName.ReadOnly = true; 18 txt_Pwd.Text = tb_users.Pwd; 19 cmb_Authority.Items.Clear();//移除下拉框中所有集合 20 cmb_Authority.Items.Add(tb_users.RoleName); 21 cmb_Authority.SelectedIndex = 0; 22 foreach (DataRow dr in dt.Rows)//遍歷數(shù)據(jù)表中行的數(shù)據(jù) 23 { 24 if (dr[0].ToString()== tb_users.RoleName) 25 { 26 continue; 27 } 28 cmb_Authority.Items.Add(dr[0]);//把表中第一列的數(shù)據(jù)添加到下拉框中 29 } 30 31 } 1 string sql = "select * from tb_Role"; //查詢數(shù)據(jù)所有信息 2 DataTable dt = SqlHelper.DataTable(sql); 3 cmb_Authority.DataSource = dt; //下拉框數(shù)據(jù)源為dt 4 cmb_Authority.DisplayMember = "RoleName";//顯示成員 5 cmb_Authority.ValueMember = "RoleID"; //值成員(類似與索引) 6 7 //cmb_Authority.Items.Clear();//移除下拉框中所有集合 8 //foreach (DataRow dr in dt.Rows)//遍歷數(shù)據(jù)表中行的數(shù)據(jù) 9 //{ 10 // cmb_Authority.Items.Add(dr[0]);//把表中第一列的數(shù)據(jù)添加到下拉框中 11 //} 12 //if (xiu==0) 13 //{ 14 // cmb_Authority.SelectedIndex = 0; 15 //} 16 if (xiu==1) 17 { 18 btn_Reg.Text = "修改"; 19 txt_OperatorName.Text = tb_users.OperatorName; 20 txt_UserName.Text = tb_users.UserName; 21 txt_UserName.ReadOnly = true; 22 txt_Pwd.Text = tb_users.Pwd; 23 cmb_Authority.SelectedValue = tb_users.RoleID; //(下拉框的值=這個(gè)索引) 24 } 一個(gè)是書庫(kù)綁定 一個(gè)是再次循環(huán)賦值
兩種賦值方法 comboBox1.Items.Add(new { key = "key", value = "value" }); ArrayList list = new ArrayList(); list.Add(new DictionaryEntry("name", "姓名")); list.Add(new DictionaryEntry("username", "用戶名")); list.Add(new DictionaryEntry("sfz", "身份Y證號(hào)")); list.Add(new DictionaryEntry("Tel", "電話號(hào)碼")); comboBox1.DataSource = list; comboBox1.DisplayMember = "Value";//顯示出來的。Text comboBox1.ValueMember = "Key";// value值。
|
|