winform —— listview创建表及简单的增删改查
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Linq; 7 using System.Text; 8 using System.Windows.Forms; 9 using WindowsFormsApplication7.App_Code; 10 11 namespace WindowsFormsApplication7 12 { 13 public partial class Form4 : Form 14 { 15 public Form4() 16 { 17 InitializeComponent(); 18 } 19 //新增 20 private void button1_Click(object sender, EventArgs e) 21 { 22 Insert f = new Insert();//打开Insert新增窗体 23 f.Owner = this; 24 f.Show(); 25 } 26 //窗体一运行就执行 27 private void Form4_Load(object sender, EventArgs e) 28 { 29 List<UserData> list = new UserDA().Select();//查询数据库对应数据 30 Binddata(list);//绑定数据 31 } 32 33 private void Binddata(List<UserData> list) 34 { 35 //第一步:构建listview"' 36 listView1.Columns.Clear(); 37 listView1.Columns.Add("编号"); 38 listView1.Columns.Add("用户名"); 39 listView1.Columns.Add("密码"); 40 41 listView1.Items.Clear(); 42 43 //读取数据,通过数据构建listview中的行 44 45 foreach (UserData data in list) 46 { 47 ListViewItem lt = new ListViewItem(); 48 lt.Text = data.Code.ToString(); 49 lt.SubItems.Add(data.Uname); 50 lt.SubItems.Add(data.UPass); 51 52 listView1.Items.Add(lt); 53 } 54 } 55 //条件查询 56 private void button4_Click(object sender, EventArgs e) 57 { 58 List<UserData> listtiaojian = new UserDA().Select(textBox1.Text); 59 Binddata(listtiaojian); 60 } 61 //删除数据 62 private void button3_Click(object sender, EventArgs e) 63 { 64 if (listView1.SelectedItems.Count > 0) 65 { 66 string code = listView1.SelectedItems[0].Text; 67 new UserDA().Delete(code); 68 //重新绑定数据 69 List<UserData> list = new UserDA().Select(); 70 Binddata(list); 71 } 72 else 73 { 74 MessageBox.Show("请选择,再删除"); 75 } 76 } 77 //修改数据 78 private void button2_Click(object sender, EventArgs e) 79 { 80 if (listView1.SelectedItems.Count > 0) 81 { 82 UserData data = new UserData(); 83 data.Code = int.Parse(listView1.SelectedItems[0].SubItems[0].Text); 84 data.Uname = listView1.SelectedItems[0].SubItems[1].Text; 85 data.UPass = listView1.SelectedItems[0].SubItems[2].Text; 86 Update f = new Update(data); 87 f.Show(); 88 } 89 else 90 { 91 MessageBox.Show("请选择,再修改"); 92 } 93 94 95 } 96 } 97 }