winform:对dataGridView绑定的泛型ListT的简单CRUD
1. 创建对象类,为所有成员封装字段,然后重载该类;
2. 根据已有的对象类(类型参数)创建⼀个长度可以变化的List数组,并绑定数据源;
3. 设置dataGridView的column属性:对应四个对象类创建相应列并⽤Name属性进⾏绑定;
4. button_onclick事件点击处理;
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
List<Person> L1 = new List<Person>();
private List<Person> L11 = new List<Person>();
public List<Person> L111
{
get { return L11; }
set { L11 = value; }
}
//internal
private void Form1_Load(object sender, EventArgs e)
{
//学号,姓名,年龄,电话
L111.Add(new Person("1", "王", 4, "466666666666"));
L111.Add(new Person("2", "温", 5, "4444444"));
L111.Add(new Person("3", "诏", 4, "4444444444"));
L111.Add(new Person("4", "伦", 4, "4555555"));
//this.dataGridView1.DataSource = L1;
LoadData(L111);
}
public void LoadData(List<Person> L1)
{
this.dataGridView1.DataSource = new BindingList<Person>(L1);
}
/
/Add
private void button2_Click(object sender, EventArgs e)
{
Add a1 = new Add(this);
a1.Show();
}
//删除
private void button4_Click(object sender, EventArgs e)
{
if (dataGridView1.SelectedRows.Count <= 0)
{
return;
}
int ind = this.dataGridView1.CurrentRow.Index;
L111.RemoveAt(ind);
LoadData(L111);
}
//退出
private void button6_Click(object sender, EventArgs e)
{
Application.Exit();
}
/
/修改
private void button3_Click(object sender, EventArgs e)
{
//Edit e1 = new Edit(this);
//e1.Show();
Person per = this.dataGridView1.CurrentRow.DataBoundItem as Person;            Add ad = new Add(this, per);
ad.ShowDialog();
;
}
//刷新
private void button5_Click(object sender, EventArgs e)
{
LoadData(L111);
}
//查询
private void button1_Click(object sender, EventArgs e)
{
List<Person> L2 = new List<Person>();
//学号,姓名,年龄,电话
foreach (Person item in L11)
{
if(item.Name.Contains(textBox1.Text))
{
L2.Add(item);
}
}
LoadData(L2);
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
}
public class Person
{
public Person() { }
public Person(string no, string name, int age, string phone) {
this.No = no;
this.Name = name;
this.Age = age;
this.Phone = phone;
}
//学号
private string no;
public string No
{
get { return no; }
set { no = value; }
}
//    姓名
datagridview数据源private string name;
public string Name
{
get { return name; }
set { name = value; }
}
/
/    年龄
private int age;
public int Age
{
get { return age; }
set { age = value; }
}
//    电话
private string phone;
public string Phone
{
get { return phone; }
set { phone = value; }
}
}
public partial class Add : Form
{
public Add()
{
InitializeComponent();
}
//Add
Form1 f;
public Add(Form1 f)
{
InitializeComponent();
this.f = f;
button1.Text = "Add";
}
//学号,姓名,年龄,电话
int index;
//Edit
public Add(Form1 f, Person per)
{
InitializeComponent();
this.f = f;
textBox1.Text = per.No;
textBox2.Text = per.Name;
textBox3.Text = per.Age.ToString();
textBox4.Text = per.Phone;
button1.Text = "Edit";
index = f.dataGridView1.CurrentRow.Index;
}
private void button1_Click(object sender, EventArgs e)
/
/⾮空验证
if (textBox1.Text == string.Empty || textBox2.Text == string.Empty || textBox3.Text == string.Empty || textBox4.Text == string.Empty)            {
MessageBox.Show("亲,记得输⼊数据哦");
}
else
{
//验证学号是否重复
foreach (Person item in f.L111)
{
if (item.No == textBox1.Text)
{
if (button1.Text == "Edit" && item.No != textBox1.Tag.ToString().Trim() || button1.Text == "Add")
{
MessageBox.Show("学号已存在!");
return;
}
}
}
//修改数据源并重新绑定到DataGridView
Person p1 = new Person(textBox1.Text.Trim(), textBox2.Text.Trim(), int.Parse(textBox3.Text.Trim()), textBox4.Text.Trim());
if (button1.Text == "Add")
{
f.L111.Add(p1);
}
else
{
f.L111[index] = p1;
textBox1.Tag = p1.No;
}
f.LoadData(f.L111);
//f.LoadData(L11);
}
}
private void textBox4_TextChanged(object sender, EventArgs e)
{
}
private void Add_Load(object sender, EventArgs e)
{
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
this.Close();
}
对应的界⾯:
还有就是新增和修改调⽤的界⾯的都是Add。所以会看到在Add开头那⾥有⼏个重载,然后会根据不同的 button1.Text执⾏相对应的操作。还有⼀种是⽤winform 对dictionary进⾏操作。
dataGridView数据绑定是this.dataGridView.DataSource = new BindingList<T>();
还有定义⼀个数组去储存某⼀列的值,⽐如下⾯这两句:
string content = File.ReadAllText(Application.StartupPath + "//");
string[] values = Regex.Split(content, "\r\n");
其他列可以⽤随机数随便搞定,其他的都差不多。
最后感谢⼀下我那个外星⼈同学,版权归他所有~

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。