WinForm中CheckListBox控件的应用

来源:互联网 发布:删除列sql语句 编辑:程序博客网 时间:2024/05/19 06:49

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

原创粉丝点击