DataGridView绑定数据、删除数据

2021-05-22 12:30

阅读:545

标签:obj   清空   range   lag   let   demo   form   object   todo   

定义学生类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GridViewDemo
{
    public class Student
    {
        //姓名
        private string Name;
        public string name
        {
            get
            {
                return Name;
            }
            set
            {
                Name = value;
            }
        }
        //得分
        private double Score;
        public double score
        {
            get
            {
                return Score;
            }
            set
            {
                Score = value;
            }
        }
    }
}

代码实现:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace GridViewDemo
{
    public partial class Form1 : Form
    {
        //定义全局对象
        List studenList = new List();
        DataTable dt = new DataTable();

        public Form1()
        {
            InitializeComponent();
        }

        #region 创建集合类实现建立并存放20个学生的方法,类似于创建班级类。
        private List GeneraStudent()
        {
            List studentLists = new List() ;
            //真正去创建20个学生
            for (int i=1; i21;i++)
            {
                Student student = new Student();
                student.name = "student"+i;
                Random rad = new Random();
                System.Threading.Thread.Sleep(100);
                student.score =Convert.ToDouble( Convert.ToDouble(rad.Next(101) + (0.5 * (i % 2))).ToString("000.0"));
                studentLists.Add(student);
            }
            return studentLists;
        }
        #endregion

        #region 绑定到列表控件
        private void bangdingstudentOnGridView(int flag)
        {
            if (flag == 0)
            {                          
            //表格清空初始化
            this.dgv_Students.Rows.Clear();
            this.dgv_Students.Columns.Clear();

            //实例化表格
            dt = new DataTable();

            //实例化每列 并初始化列名称和列数据类型
            DataColumn dc = new DataColumn();
            dc.ColumnName = "Name";
            //dc.DataType = Type.GetType("string");
            dt.Columns.Add(dc);

            DataColumn dcvalue = new DataColumn();
            dcvalue.ColumnName = "Score";
            //dcvalue.DataType = Type.GetType("int");
            dt.Columns.Add(dcvalue);

            //调用创建学生方法,创建20个学生。
            studenList = this.GeneraStudent();
            //把创建的20个学生用循环方法加载到每行
            foreach (Student student in studenList )
            {
                //实例化表格行 并加载创建出的学生信息
                DataRow dr = dt.NewRow();
                dr["Name"] = student.name;
                dr["Score"] = student.score;
                dt.Rows.Add(dr);
            }
            //把数据绑定到表格控件数据源
            this.dgv_Students.DataSource = dt;
                
            DataGridViewLinkColumn dlc = new DataGridViewLinkColumn();
            dlc.Name = "Delete";
            dlc.Text = "Delete";
            this.dgv_Students.Columns.AddRange(new DataGridViewColumn[] { dlc });

            foreach (DataGridViewRow dr in this.dgv_Students.Rows)
              {
                 dr.Cells[2].Value = "Delete";
              }
            }
            else
            {
                ////表格清空初始化
                //this.dgv_Students.Rows.Clear();
                //this.dgv_Students.Columns.Clear();
                dt.Rows.Clear();

                //实例化表格
                //DataTable dt = new DataTable();

                ////实例化每列 并初始化列名称和列数据类型
                //DataColumn dc = new DataColumn();
                //dc.ColumnName = "Name";
                ////dc.DataType = Type.GetType("string");
                //dt.Columns.Add(dc);

                //DataColumn dcvalue = new DataColumn();
                //dcvalue.ColumnName = "Score";
                ////dcvalue.DataType = Type.GetType("int");
                //dt.Columns.Add(dcvalue);

                //把创建的20个学生用循环方法加载到每行
                foreach (Student student in studenList)
                {
                    //实例化表格行 并加载创建出的学生信息
                    DataRow dr = dt.NewRow();
                    dr["Name"] = student.name;
                    dr["Score"] = student.score;
                    dt.Rows.Add(dr);
                }
                //把数据绑定到表格控件数据源
                this.dgv_Students.DataSource = dt;

                foreach (DataGridViewRow dr in this.dgv_Students.Rows)
                {
                    dr.Cells[2].Value = "Delete";
                }
            }

        }
        #endregion



        private void Form1_Load(object sender, EventArgs e)
        {
            this.bangdingstudentOnGridView(0);
        }

        private void dgv_Students_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex  == 2)
            {
                //获取选中行的数据ID
                string strStudentName = this.dgv_Students.Rows[e.RowIndex].Cells[0].Value.ToString();
                foreach (Student st in studenList )
                {
                    if(st.name.Equals(strStudentName))
                    {
                        //删除
                        studenList.Remove(st);
                        this.bangdingstudentOnGridView(1);
                        return;
                    }
                }
            }
        }
    }
}

 

DataGridView绑定数据、删除数据

标签:obj   清空   range   lag   let   demo   form   object   todo   

原文地址:https://www.cnblogs.com/palebluestarrysky/p/11192533.html


评论


亲,登录后才可以留言!