1. 创建对象类,为所有成员封装字段,然后重载该类;
  2. 根据已有的对象类(类型参数)创建一个长度可以变化的List数组,并绑定数据源;
  3. 设置dataGridView的column属性:对应四个对象类创建相应列并用Name属性进行绑定;
  4. button_onclick事件点击处理;

public partial class Form1 : Form

  1.     {
  2.         public Form1()
  3.         {
  4.             InitializeComponent();
  5.         }
  6.               
  7.  
  8.         List<Person> L1 = new List<Person>();
  9.         private List<Person> L11 = new List<Person>();
  10.  
  11.         public List<Person> L111
  12.         {
  13.             get { return L11; }
  14.             set { L11 = value; }
  15.         }
  16.  
  17.          //internal
  18.  
  19.         private void Form1_Load(object sender, EventArgs e)
  20.         {
  21.  
  22.             //学号,姓名,年龄,电话
  23.             L111.Add(new Person("", "王尼玛", , ""));
  24.             L111.Add(new Person("", "温尼玛", , ""));
  25.             L111.Add(new Person("", "诏尼玛", , ""));
  26.             L111.Add(new Person("", "伦尼玛", , ""));
  27.             //this.dataGridView1.DataSource = L1;
  28.             LoadData(L111);
  29.  
  30.         }
  31.  
  32.         public void LoadData(List<Person> L1)
  33.         {
  34.             this.dataGridView1.DataSource = new BindingList<Person>(L1);
  35.             
  36.         }
  37.         //Add
  38.         private void button2_Click(object sender, EventArgs e)
  39.         {
  40.  
  41.             Add a1 = new Add(this);
  42.             a1.Show();
  43.         }
  44.  
  45.         //删除
  46.         private void button4_Click(object sender, EventArgs e)
  47.         {
  48.             if (dataGridView1.SelectedRows.Count <= )
  49.             {
  50.                 return;
  51.             }
  52.             int ind = this.dataGridView1.CurrentRow.Index;
  53.             L111.RemoveAt(ind);
  54.             LoadData(L111);
  55.         }
  56.         //退出
  57.         private void button6_Click(object sender, EventArgs e)
  58.         {
  59.             Application.Exit();
  60.         }
  61.         //修改
  62.         private void button3_Click(object sender, EventArgs e)
  63.         {
  64.             //Edit e1 = new Edit(this);
  65.             //e1.Show();
  66.             Person per = this.dataGridView1.CurrentRow.DataBoundItem as Person;
  67.             Add ad = new Add(this, per);
  68.             ad.ShowDialog();
  69.             ;
  70.  
  71.         }
  72.         //刷新
  73.         private void button5_Click(object sender, EventArgs e)
  74.         {
  75.             LoadData(L111);
  76.         }
  77.         
  78.         //查询
  79.         private void button1_Click(object sender, EventArgs e)
  80.         {
  81.  
  82.             List<Person> L2 = new List<Person>();
  83.             //学号,姓名,年龄,电话
  84.             foreach (Person item in L11)
  85.             {
  86.                 if(item.Name.Contains(textBox1.Text))
  87.                 {
  88.                     L2.Add(item);
  89.                 }
  90.             }
  91.             LoadData(L2);
  92.             
  93.         }
  94.  
  95.         private void textBox1_TextChanged(object sender, EventArgs e)
  96.         {
  97.  
  98.         }
  99.  
  100.     }

public class Person

  1.     {
  2.         public Person() { }
  3.         public Person(string no, string name, int age, string phone) {
  4.             this.No = no;
  5.             this.Name = name;
  6.             this.Age = age;
  7.             this.Phone = phone;
  8.             
  9.         }
  10.         //学号
  11.         private string no;
  12.  
  13.         public string No
  14.         {
  15.             get { return no; }
  16.             set { no = value; }
  17.         }
  18.         //    姓名
  19.         private string name;
  20.  
  21.         public string Name
  22.         {
  23.             get { return name; }
  24.             set { name = value; }
  25.         }
  26.         //    年龄
  27.         private int age;
  28.  
  29.         public int Age
  30.         {
  31.             get { return age; }
  32.             set { age = value; }
  33.         }
  34.         //    电话
  35.         private string phone;
  36.  
  37.         public string Phone
  38.         {
  39.             get { return phone; }
  40.             set { phone = value; }
  41.         }
  42.     }
  1.  public partial class Add : Form
  2.     {
  3.         public Add()
  4.         {
  5.             InitializeComponent();
  6.  
  7.         }
  8.         //Add
  9.         Form1 f;
  10.         public Add(Form1 f)
  11.         {
  12.             InitializeComponent();
  13.             this.= f;
  14.             button1.Text = "Add";
  15.         }
  16.  
  17.         //学号,姓名,年龄,电话
  18.  
  19.         int index;
  20.         //Edit
  21.         public Add(Form1 f, Person per)
  22.         {
  23.             InitializeComponent();
  24.             this.= f;
  25.             textBox1.Text = per.No;
  26.             textBox2.Text = per.Name;
  27.             textBox3.Text = per.Age.ToString();
  28.             textBox4.Text = per.Phone;
  29.             button1.Text = "Edit";
  30.             index = f.dataGridView1.CurrentRow.Index;
  31.         }
  32.  
  33.         private void button1_Click(object sender, EventArgs e)
  34.         {
  35.             //非空验证
  36.             if (textBox1.Text == string.Empty || textBox2.Text == string.Empty || textBox3.Text == string.Empty || textBox4.Text == string.Empty)
  37.             {
  38.                 MessageBox.Show("亲,记得输入数据哦");
  39.  
  40.             }
  41.             else
  42.             {
  43.                 //验证学号是否重复
  44.                 foreach (Person item in f.L111)
  45.                 {
  46.                     if (item.No == textBox1.Text)
  47.                     {
  48.  
  49.                         if (button1.Text == "Edit" && item.No != textBox1.Tag.ToString().Trim() || button1.Text == "Add")
  50.                         {
  51.                             MessageBox.Show("学号已存在!");
  52.                             return;
  53.                         }
  54.                     }
  55.                 }
  56.  
  57.                 //修改数据源并重新绑定到DataGridView
  58.                 Person p1 = new Person(textBox1.Text.Trim(), textBox2.Text.Trim(), int.Parse(textBox3.Text.Trim()), textBox4.Text.Trim());
  59.                 
  60.                 if (button1.Text == "Add")
  61.                 {
  62.                     f.L111.Add(p1);
  63.                 }
  64.                 else
  65.                 {
  66.                     f.L111[index] = p1;
  67.                     textBox1.Tag = p1.No;
  68.                 }
  69.                 f.LoadData(f.L111);
  70.  
  71.                 
  72.  
  73.                 //f.LoadData(L11);
  74.  
  75.             }
  76.         }
  77.  
  78.         private void textBox4_TextChanged(object sender, EventArgs e)
  79.         {
  80.  
  81.         }
  82.  
  83.         private void Add_Load(object sender, EventArgs e)
  84.         {
  85.  
  86.         }
  87.  
  88.         private void textBox1_TextChanged(object sender, EventArgs e)
  89.         {
  90.  
  91.         }
  92.  
  93.         private void button2_Click(object sender, EventArgs e)
  94.         {
  95.             this.Close();
  96.         }

}

对应的界面:

还有就是新增和修改调用的界面的都是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. [Xamarin]我的Xamarin填坑之旅(二)

    上一篇交代了我Xamarin填坑的背景,大概聊了聊第一步环境配置,第二步创建项目和开发框架选择.如果有一个可用的梯子,这部分基本不会出错. 接下来就具体聊一聊写代码的过程中遇到的一些事儿. 第三步是码 ...

  2. sqlServer数据库纵横表相互转化

    sqlServer  数据库纵横表相互转化 一.纵表转横表: 1.纵表: 2.横表: 3. 代码: select Name as '姓名', end) as '语文', end) as '数学', e ...

  3. click 版本升级7.0踩过的坑

    click 版本升级7.0踩过哪些坑? click 版本6.7升级至7.0以上,包名由 click 变更为 Click click 的 Options 和 Parameters 规则变更为如下: Fo ...

  4. 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& ...

  5. 关于Boolean()

    Boolean(value); 如果省略 value 参数,或者设置为 0.-0.null."".false.undefined 或 NaN,则该对象设置为 false. 否则设置 ...

  6. 页面按钮埋点+跟踪location.search

    <a href="javascript: void(0)" onclick="setUrl('https://baoxian.pingan.com/pa18shop ...

  7. 八、Linux上常用网络操作

    1. 主机名配置 hostname 查看主机名 hostname xxx 修改主机名 重启后无效 如果想要永久生效,可以修改/etc/sysconfig/network文件 2. IP地址配置 set ...

  8. [webrtc] 强制使用tcp传输

    以前笔记,整理 webrtc默认使用UDP传输,但是也可以通过TCP传输. 使用tcp传输,需要服务器中转,turnserver,licode,janus之类的服务器. 1. 如果使用turnserv ...

  9. Linux的帮助文档命令

    Linux的帮助文档命令 1.man page man是manual(操作手册)的简写,使用方式: man [指令] man date 在显示的内容中查找内容: / + 搜索你的关键字 上下左右键来查 ...

  10. Linux vim 编辑命令

    vi命令命令模式:yy:复制 光标所在的这一行 4yy:复制 光标所在行开始向下的4行p: 粘贴dd:剪切 光标所在的这一行2dd:剪切 光标所在行 向下 2行D:从当前的光标开始剪切,一直到行末d0 ...