首页 > 代码库 > 从listView1中选择记录到listView2中

从listView1中选择记录到listView2中

1.自定义Worker字段类

    public class SelectWorker
    {
        public int WorkerID;
        public string WkName;
    }

2.定义全局变量

        List<SelectWorker> wkList0 = new List<SelectWorker>();
        List<SelectWorker> wkList1 = new List<SelectWorker>();
        List<SelectWorker> wkList2 = new List<SelectWorker>();
 
        int selRow0 = -1, selRow1 = -1;
        int selTRow0 = -1, selTRow1 = -1;
3.初始化listView函数

        void InitListView12()
        {
            listView1.View = View.Details;
            ColumnHeader ch1 = new ColumnHeader();
            ColumnHeader ch2 = new ColumnHeader();
            ch1.Text = "编码";
            ch2.Text = "姓名";
            listView1.Columns.Add(ch1);
            listView1.Columns.Add(ch2);

            listView2.View = View.Details;
            ColumnHeader ch4 = new ColumnHeader();
            ColumnHeader ch5 = new ColumnHeader();
            ch4.Text = "编码";
            ch5.Text = "姓名";
            listView2.Columns.Add(ch4);
            listView2.Columns.Add(ch5);
        }

4.窗体加载事件

        private void Form1_Load(object sender, EventArgs e)
        {
            string SQL;
            InitListView12();
            MyDBase DB = new MyDBase(DBUser.sServer, DBUser.sDBName, DBUser.sUser, DBUser.sPasswd);
            SQL = "SELECT workerID,wkName FROM worker order by workerID";
            SqlDataReader RD0 = DB.DBDataReader(SQL);
            while (RD0.Read())
            {
                SelectWorker SW = new SelectWorker();
                SW.WorkerID = int.Parse(RD0["WorkerID"].ToString());
                SW.WkName = RD0["WkName"].ToString();
                wkList0.Add(SW);
                wkList2.Add(SW);  
                ListViewItem lvi = new ListViewItem();
                lvi.Text = SW.WorkerID;
                lvi.SubItems.Add(SW.WkName);
                listView1.Items.Add(lvi);
            }
            RD0.Close();
            DB.DBClose();
        }
5.向listView1和listView2中加减记录
        void LoadInListView12()
        {
            int i, n;
            listView1.Items.Clear();
            listView2.Items.Clear();
            InitListView12();
            n = wkList0.Count;
            for (i = 0; i < n; i++)
            {
                ListViewItem lvi = new ListViewItem();
                lvi.Text=wkList0[i].WorkerID.ToString();
                lvi.SubItems.Add(wkList0[i].WkName);
                listView1.Items.Add(lvi);
            }
            n = wkList1.Count;
            for (i = 0; i < n; i++)
            {   
                ListViewItem lvi = new ListViewItem();
                lvi.Text=wkList0[i].WorkerID.ToString();
                lvi.SubItems.Add(wkList0[i].WkName);
                listView1.Items.Add(lvi);
             }
         }

6.listView1和listView的选择事件

        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            int length = listView1.SelectedItems.Count;
            for (int i = 0; i < length; i++)
                selRow0 = listView1.SelectedItems[i].Index; 
        }

        private void listView2_SelectedIndexChanged(object sender, EventArgs e)
        {
            int length = listView2.SelectedItems.Count;
            for (int i = 0; i < length; i++)
                selRow1 = listView2.SelectedItems[i].Index; 
        }

7.添加员工

        private void button1_Click(object sender, EventArgs e)
        {
            if (selRow0 == -1)
            {
                MessageBox.Show("请选择一个职工", "没有选择职工提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            SelectWorker SelWK = new SelectWorker();
            SelWK = wkList0[selRow0];
            wkList0.Remove(SelWK);
            wkList1.Add(SelWK);
            LoadInListView12();
            selRow0 = -1; 
        }

8.减掉员工

        private void button2_Click(object sender, EventArgs e)
        {
            if (selRow1 == -1)
            {
                MessageBox.Show("请选择一个职工", "没有选择职工提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            SelectWorker SelWK = new SelectWorker();
            SelWK = wkList1[selRow1];
            wkList1.Remove(SelWK);
            wkList0.Add(SelWK);
            LoadInListView12();
            selRow1 = -1; 
        }




从listView1中选择记录到listView2中