首页 > 代码库 > 从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中
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。