winform —— listview创建表及简单的增删改查

winform  —— listview创建表及简单的增删改查

 using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WindowsFormsApplication7.App_Code; namespace WindowsFormsApplication7
{
public partial class Form4 : Form
{
public Form4()
{
InitializeComponent();
}
//新增
private void button1_Click(object sender, EventArgs e)
{
Insert f = new Insert();//打开Insert新增窗体
f.Owner = this;
f.Show();
}
//窗体一运行就执行
private void Form4_Load(object sender, EventArgs e)
{
List<UserData> list = new UserDA().Select();//查询数据库对应数据
Binddata(list);//绑定数据
} private void Binddata(List<UserData> list)
{
//第一步:构建listview"'
listView1.Columns.Clear();
listView1.Columns.Add("编号");
listView1.Columns.Add("用户名");
listView1.Columns.Add("密码"); listView1.Items.Clear(); //读取数据,通过数据构建listview中的行 foreach (UserData data in list)
{
ListViewItem lt = new ListViewItem();
lt.Text = data.Code.ToString();
lt.SubItems.Add(data.Uname);
lt.SubItems.Add(data.UPass); listView1.Items.Add(lt);
}
}
//条件查询
private void button4_Click(object sender, EventArgs e)
{
List<UserData> listtiaojian = new UserDA().Select(textBox1.Text);
Binddata(listtiaojian);
}
//删除数据
private void button3_Click(object sender, EventArgs e)
{
if (listView1.SelectedItems.Count > )
{
string code = listView1.SelectedItems[].Text;
new UserDA().Delete(code);
//重新绑定数据
List<UserData> list = new UserDA().Select();
Binddata(list);
}
else
{
MessageBox.Show("请选择,再删除");
}
}
//修改数据
private void button2_Click(object sender, EventArgs e)
{
if (listView1.SelectedItems.Count > )
{
UserData data = new UserData();
data.Code = int.Parse(listView1.SelectedItems[].SubItems[].Text);
data.Uname = listView1.SelectedItems[].SubItems[].Text;
data.UPass = listView1.SelectedItems[].SubItems[].Text;
Update f = new Update(data);
f.Show();
}
else
{
MessageBox.Show("请选择,再修改");
} }
}
}
上一篇:Centos7 yum安装nginx


下一篇:Centos7下的shadow暴力破解