WinForm中CheckListBox控件的应用

CheckedListBox如何绑定数据库显示数据项?

//加载字段到checkboxlist
IList<string> list1 = dataop.GetTableColumnsName(e.Node.Parent.Name);//调用
for (int j = 0; j < list1.Count; j++)
 {
      chklColumn.Items.Add(list1[j], true);  //添加显示字段,作为多选项

      //其中true表示默认均选择
 }

********************************

 #region 获取表的字段名称 //链接数据库的内容代码省略
        public IList<string> GetTableColumnsName(string tablename)
        {
            SqlConnection conn;
            DataTable dt;
            GetTableComm(tablename, out conn, out dt);
            IList<string> list1 = new List<string>();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                list1.Add(dt.Rows[i]["字段名"].ToString());
            }
            conn.Close();
            return list1;
        }
        #endregion

**********************************

【【【【【【【【【【【华丽丽的分割线【【【【【【【【【【【【【【【

《1》this.checklistboxUser.DataSource = ds.Tables["table1"]; //adp.Fill(ds,"table1")

//看情况,或者ds.Tables[0]; 其他是那样的adp.Fill(ds),低级的就不说了 

this.checklistboxUser.DisplayMember = "UserName";  

this.checklistboxUser.ValueMember = "UserID";

《2》如何获取checkedlistBox的DisplayMember和ValueMember 

for(int i=0; i< checklbUser.Items.Count; i++)

{

if(checklbUser.GetItemChecked(i)==true)

{

MessageBox.Show(checklbUser.GetItemText(checklbUser.Items[i]));//获取DisplayMember

MessageBox.Show(ds.Tables["table1"].Rows[i]["UserID"].ToString() );//获取ValueMember 

}

}

《3》获取所有选中项

         if (this.checkedListBox1.CheckedItems.Count > 0) 
            { 
                foreach (string item in this.checkedListBox1.CheckedItems
                { 
                    //item就是你要插入数据库的数据    
                } 

《4》一些可以借鉴的例子

  //从另一张数据库表里面获得一个用户的所有角色并让checkListBox里面的对应项选中。【显示】
         public  void  checkBind()
         {
             ds = dataoperate.getDs( "select roletype from role_table where userid='"  + userid +  "'" "role_table" );
             foreach (DataRow dr in ds.Tables[0].Rows)
            {
                int i = this.checkedListBox1.FindString(dr["roletype"].ToString());
                if(i>=0)
                {
                    this.checkedListBox1.SetItemChecked(i,true);
                }
            }
         }
         
         //根据的CheckListBox里面的选中项来更新用户的角色。【更改】
           public  void  updatRole()
          {
             DeleteRoleById(userid);  //先删除用户的所有角色,以免重复
             string  struserid;
            string  strusertype;
            for ( int  i=0;i< this .checkedListBox1.CheckedItems.Count;i++)
            {
              string   roletype  =  this .checkedListBox1.CheckedItems[i].ToString();
             //插入操作
insert into rol_table (userid,roletype) values('" + struserid + "','" + strusertype + "')");
            }
          }
》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》
 #region 获取CheckedlistBox选中的字段名称
        private string GetChklColumn()
        {
            string strColumn = "";
            for (int i = 0; i < chklColumn.CheckedItems.Count; i++)
            {
                strColumn += "'"+chklColumn.CheckedItems[i].ToString() + "',";
            }
            if (strColumn.Length > 0)
            {
                strColumn = strColumn.Remove(strColumn.Length - 1, 1);
            }
            else
            {
                strColumn = "''";
            }
            return strColumn;
        }
        #endregion

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值