Combobox 下拉框赋值

 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)//遍历数据表中行的数据
 5             {
 6                 cmb_Authority.Items.Add(dr[0]);//把表中第一列的数据添加到下拉框中
 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)//遍历数据表中行的数据
23                 {
24                     if (dr[0].ToString()== tb_users.RoleName)
25                     {
26                         continue;
27                     }
28                     cmb_Authority.Items.Add(dr[0]);//把表中第一列的数据添加到下拉框中
29                 }
30 
31             }
 1 string sql = "select * from tb_Role"; //查询数据所有信息
 2             DataTable dt = SqlHelper.DataTable(sql);
 3             cmb_Authority.DataSource = dt; //下拉框数据源为dt
 4             cmb_Authority.DisplayMember = "RoleName";//显示成员
 5             cmb_Authority.ValueMember = "RoleID"; //值成员(类似与索引)
 6 
 7             //cmb_Authority.Items.Clear();//移除下拉框中所有集合
 8             //foreach (DataRow dr in dt.Rows)//遍历数据表中行的数据
 9             //{
10             //    cmb_Authority.Items.Add(dr[0]);//把表中第一列的数据添加到下拉框中
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; //(下拉框的值=这个索引)
24             }

一个是书库绑定 一个是再次循环赋值

转载于:https://www.cnblogs.com/enych/p/7607417.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值