POC on Grid View Add item to list,Delete row

POC on Grid View Add item to list,Delete row

1) create an Employee List
2)Add TextBox to enter the details
3)Add Employee details in the Grid View as a new row
4)Add CheckBox to select the list
5)Remove the Checked list.


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 GridView
{

   

    public partial class Form1 : Form
    {

        EmployeeController ec = new EmployeeController();
        DataGridViewCheckBoxColumn checkbox = new DataGridViewCheckBoxColumn();
        public Form1()
        {

           
            InitializeComponent();
            ec.initialEmployee();
            dataGridView2.DataSource = ec.getEmployeeList();
           
            checkbox.HeaderText = " ";
            checkbox.Width = 30;
            checkbox.Name = "CheckBox";
            dataGridView2.Columns.Insert(0, checkbox);

       
        }



       
        private void button3_Click(object sender, EventArgs e)
        {
            string name = textBox1.Text;
            string id = textBox2.Text;
            int salary = int.Parse(textBox3.Text);
            Employee emp = new Employee { name = name, Id = id, Salary = salary };
            ec.addEmployee(emp);
            dataGridView2.DataSource = null;
            dataGridView2.DataSource = ec.getEmployeeList();

            //DataGridViewCheckBoxColumn checkbox = new DataGridViewCheckBoxColumn();
            //checkbox.HeaderText = " ";
            //checkbox.Width = 30;
            //checkbox.Name = "CheckBox";
            //dataGridView2.Columns.Insert(0, checkbox);

           
           
        }

        private void button4_Click(object sender, EventArgs e)
        {
            foreach (DataGridViewRow row in dataGridView2.Rows)
            {
                bool isSelected = Convert.ToBoolean(row.Cells["CheckBox"].Value);
                if (isSelected)
                {
                    ec.RemoveEmployee(row.Cells["name"].Value.ToString());
                }
            }

            dataGridView2.DataSource = null;
            dataGridView2.DataSource = ec.getEmployeeList();


        }
    }


    public class Employee
    {
        public string name { get; set; }
        public string Id { get; set; }
        public int Salary { get; set; }

    }


    public class EmployeeController
    {
        public List<Employee> empList  = new List<Employee>();
        public List<Employee> removeEmpList = new List<Employee>();
       
        public void initialEmployee()
        {
            Employee e1 = new Employee { name = "Empl", Id = "E01", Salary = 10000 };
            Employee e2 = new Employee { name = "Emp2", Id = "E02", Salary = 20000 };
            Employee e3 = new Employee { name = "Emp3", Id = "E03", Salary = 30000 };
            Employee e4 = new Employee { name = "Emp4", Id = "E04", Salary = 40000 };
            Employee e5 = new Employee { name = "Emp5", Id = "E05", Salary = 30000 };
            Employee e6 = new Employee { name = "Emp6", Id = "E06", Salary = 20000 };

        

            empList.Add(e1);
            empList.Add(e2);
            empList.Add(e3);
            empList.Add(e4);
            empList.Add(e5);
            empList.Add(e6);

        }

        public List<Employee> getEmployeeList()
        {
            return empList;
        }

        public void addEmployee(Employee e)
        {
           
            empList.Add(e);
        }

        public void RemoveEmployee(string name)
        {
           
            foreach(var e in empList)
            {
                if (e.name == name)
                {
                    removeEmpList.Add(e);

                }
            }

            foreach (var e in removeEmpList)
            {
                empList.Remove(e);
            }
        }

       

    }
}

Comments

Popular posts from this blog

Authentication and Authorization in Web API -Part1

My Gardening Journey 6