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("", "王尼玛", , ""));
            L111.Add(new Person("", "温尼玛", , ""));
            L111.Add(new Person("", "诏尼玛", , ""));
            L111.Add(new Person("", "伦尼玛", , ""));
            //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 <= )
            {
                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; }
        }
        //    姓名
        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 + "//User.txt");
            string[] values = Regex.Split(content, "\r\n");

其他列可以用随机数随便搞定,其他的都差不多。

最后感谢一下我那个外星人同学,版权归他所有~

winform:对dataGridView绑定的泛型List<T> 的简单CRUD的更多相关文章

  1. C# Winform中DataGridView绑定后DataGridViewCheckBoxColumn无法显示的问题

    在控件DataGridView绑定数据源后,发现DataGridViewCheckBoxColumn不能显示当前的check值.经过一番努力,现将完整代码奉献出来,仅供参考. 错误代码: /*禁止自动 ...

  2. c# winform 中DataGridView绑定List<T> 不能显示数据

    遇到问题 DataGridView绑定List后,List更新后再次绑定不显示数据 datagridview 绑定数据源的时候 用List是不能显示修改内容的..要用binginglist<T& ...

  3. WinForm DataGridView 绑定泛型List(List<T>)/ArrayList不显示的原因和解决

    背景:无意间遇到了一个不大不小的问题,希望对一些遇到的人有所帮助! 一.问题 WinForm DataGridView 绑定泛型List (List<T>)/ArrayList不显示,UI ...

  4. winform datagridview 绑定泛型集合变得不支持排序的解决方案

    原文:winform datagridview 绑定泛型集合变得不支持排序的解决方案 案例: 环境:Winform程序 控件:Datagridview 现象:Datagridview控件绑定到List ...

  5. [转]WinForm DataGridView 绑定泛型List(List<T>)/ArrayList不显示的原因和解决

    背景:无意间遇到了一个不大不小的问题,希望对一些遇到的人有所帮助! 一.问题 WinForm DataGridView 绑定泛型List (List<T>)/ArrayList不显示,UI ...

  6. DataGridView绑定泛型List时,利用BindingList来实现增删查改

    DataGridView绑定泛型List时,利用BindingList来实现增删查改  一.   DataGridView绑定泛型List的种种 1.DataGridView数据绑定对比(DataTa ...

  7. [WinForm] DataGridView 绑定 DT && ComboBox 列绑定 Dict

    一  需求介绍 一般像枚举类型的数据,我们在数据库里存储着诸如(1.2.3.4-)或者("001"."002"."003"-)此类,但是界面 ...

  8. winform中dataGridView单元格根据值设置新值,彻底解决绑定后数据类型转换的困难

    // winform中dataGridView单元格在数据绑定后,数据类型更改困难,只能迂回实现.有时候需要将数字变换为不同的文字描述,就会出现int32到string类型转换的异常,借助CellFo ...

  9. C# DataGridView绑定数据源的几种常见方式

    开始以前,先认识一下WinForm控件数据绑定的两种形式,简单数据绑定和复杂数据绑定. 1. 简单的数据绑定 例1 using (SqlConnection conn = new SqlConnect ...

随机推荐

  1. C++实现排序算法

    稳定性:快速 希尔 选择 堆排序不稳定 时间复杂度:平均情况下,快速.希尔.归并和堆排序的时间复杂度均为O(nlog2(n)),其他都是O(n^2).最坏情况下,快排的时间复杂度为O(n^2) #in ...

  2. scapy IPv6 NS NA报文构造

    NS 报文构造: #! /bin/python from scapy.all import * a=IPv6(src='2a01:4f8:161:5300::40', dst='ff02::1:ff0 ...

  3. pandas 对dataframe一列中某些值进行处理

    https://github.com/Bifzivkar/Boutique-Travel-Services-Predict/blob/master/feature/5_extract_feature. ...

  4. 931. Minimum Falling Path Sum

    Given a square array of integers A, we want the minimum sum of a falling path through A. A falling p ...

  5. jzoj3027

    根據打表找規律可得ans=c(k−n,n)∗an∗bk−nans=c(k-n,n)*a^n*b^{k-n}ans=c(k−n,n)∗an∗bk−n #include<bits/stdc++.h& ...

  6. Android逆向进阶——让你自由自在脱壳的热身运动(dex篇)

    本文作者:HAI_ 0×00 前言 来看看我们今天的主题. 让你自由自在脱壳的热身运动. 现在很多第厂家都是使用第三方的加固方式来进行加固的.或者使用自己的加固方式进行加固. 那么我们必不可少的就是脱 ...

  7. Swift 学习指引

    以下指引是基于最新的 Swift 4.0 为基础为而言. 如你在参考3.0以下版本, 那你就不要说你会 Swift, 3.0 之前是 Objective-C 的搬迁(80%),是不成熟的语言, 看着很 ...

  8. 百度地图笔记_覆盖物(标注marker,折线polyline,多边形polygon)的点击弹窗和右键菜单

    利用绘制工具绘制点线面,并在执行绘制完成回调事件给相应覆盖物添加事件操作,提供标注的点击弹窗和标注.折线.多边形的右键删除 效果图如下: 完整代码如下:html+js <!DOCTYPE htm ...

  9. Kali Linux信息收集工具全集

    001:0trace.tcptraceroute.traceroute 描述:进行路径枚举时,传统基于ICMP协议的探测工具经常会受到屏蔽,造成探测结果不够全面的问题.与此相对基于TCP协议的探测,则 ...

  10. 再学Java 之 foreach循环

    从Java 5 之后,Java提供了一种新的循环:foreach循环,这种循环遍历数组和集合更加简洁. foreach循环语法格式如下: for ( type variableName : array ...