checkedListBox控件绑定数据和获取值的代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
using PMSystem.classes;
namespace PMSystem
{
 
    public partial class frmLimit : Form
    {
        OleDbConnection MyConn;
        int lid;
        string strLimit;
        public frmLimit(int id,string aa)
        {
            InitializeComponent();
            lid = id;
            strLimit = aa;
        }

        private void frmLimit_Load(object sender, EventArgs e)
        {
            MyConn = new OleDbConnection(conn.connString);
            MyConn.Open();
            string strSel = "select *from 权限表";
            DataSet ds = new DataSet();
            OleDbDataAdapter objCommand = new OleDbDataAdapter(strSel, MyConn);
            objCommand.Fill(ds, "权限表");
            //MessageBox.Show(strLimit);
            this.checkedListBox1.DataSource = ds.Tables["权限表"];
            this.checkedListBox1.ValueMember = "lid";
            this.checkedListBox1.DisplayMember = "cname";
            //string bb = "2";
            for (int i = 0; i < checkedListBox1.Items.Count; i++)
                {
                    DataRowView dv = ((DataRowView)checkedListBox1.Items[i]);
                    string dd =dv["lid"].ToString();
                   if (strLimit.LastIndexOf(","+ dd +",")!=-1)
                   {
                        checkedListBox1.SetItemChecked(i,true);
                    }
                }
          
            MyConn.Close();
           
        }

        private void button1_Click(object sender, EventArgs e)
        {   string aa = ",";
             for   (int   i   =   0;i<checkedListBox1.CheckedItems.Count;i++)  
             {
                 DataRowView   dv   =   ((DataRowView)checkedListBox1.CheckedItems[i]);
                 aa += dv["lid"].ToString() + ",";  
             }
             //MessageBox.Show(aa);
             string sql = "Update 系统用户表 set czyqx='" + aa + "' where id=" + lid;

           //  string sql = "update 权限表 set lid=" + Convert.ToInt32(strLid) + ",cname='" + strName + "' where id=" + intID;
             OleDbCommand myComm = new OleDbCommand(sql, MyConn);
             try
             {
                 if (MyConn.State == 0)
                 {
                     MyConn.Open();
                 }
                 myComm.ExecuteNonQuery();
                 MessageBox.Show("权限设置成功!", "信息窗口", MessageBoxButtons.OK, MessageBoxIcon.Information);
                // string aaa = "select id,lid as 序号,cname as 名称 from 权限表";
                // DataBind(aaa, dataGridView1, "权限表");
             }
             catch (Exception err)
             {
                 MessageBox.Show(err.Message, "错误窗口", MessageBoxButtons.OK, MessageBoxIcon.Error);
                 return;
             }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
       
        }
    }
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值