ListBox相互移动

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using test1.ItemBase;      //添加引用
using System.Data.SqlClient;

namespace test1
{
    public partial class listBoxTest : Form
    {
        public listBoxTest()
        {
            InitializeComponent();
        }

        SqlBaseClass G_SqlClass = new SqlBaseClass();  //声明数据库操作类的对象

        private void listBoxTest_Load(object sender, EventArgs e)
        {
            string cmdText = "select ROLES_ID,ROLESNAME from TBL_USER_ROLES,TBL_ROLES where USER_ID=2 and  TBL_USER_ROLES.ROLES_ID=TBL_ROLES.ID";
            SqlDataReader dr = G_SqlClass.GetReader(cmdText);
            while (dr.Read())
            {
                string listItem = dr[0] + "." + dr[1];
                this.listBox1.Items.Add(listItem);
            }
            dr.Close();


            string cmdText1 = "select ID,ROLESNAME from TBL_ROLES where ID NOT IN (select ROLES_ID from TBL_USER_ROLES where USER_ID=2)";
            SqlDataReader dr1 = G_SqlClass.GetReader(cmdText1);
            while (dr1.Read())
            {
                string listItem1 = dr1[0] + "." + dr1[1];
                this.listBox2.Items.Add(listItem1);
            }
            dr.Close();
        }

        private void btn_moveRight_Click(object sender, EventArgs e)
        {
            if (this.listBox1.SelectedItem != null)
            {
                this.listBox2.Items.Add(this.listBox1.SelectedItem);
                this.listBox1.Items.Remove(this.listBox1.SelectedItem);
            }
        }

        private void btn_moveLeft_Click(object sender, EventArgs e)
        {
            if (this.listBox2.SelectedItem != null)
            {
                this.listBox1.Items.Add(this.listBox2.SelectedItem);
                this.listBox2.Items.Remove(this.listBox2.SelectedItem);
            }
        }

        private void btn_confirm_Click(object sender, EventArgs e)
        {
            string cmdText = "delete from TBL_USER_ROLES where USER_ID=2";
            G_SqlClass.GetExecute(cmdText);
            bool flag = false;
            for (int i = 0; i < this.listBox1.Items.Count; i++)
            {
                int itemCode = Convert.ToInt32(this.listBox1.Items[i].ToString().Substring(0, 1));
                string cmdText1 = "insert into TBL_USER_ROLES(USER_ID,ROLES_ID) values('2','" + itemCode + "')";
                flag=G_SqlClass.GetExecute(cmdText1);

            }
            if (flag == true)
            {
                MessageBox.Show("权限设置成功");
            }           
        }
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值