本文介绍了使用通用集合的EmployeeMaster的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用Datatable,数据集和datagrids制作了EmployeeMaster应用程序,但是现在我想使用Generic Collections进行创建,所以有人可以帮助我吗?

我想为自己的代码完整地转换代码....
以下是EmployeeMaster的我的代码...

Form1.cs


I had made an EmployeeMaster Application by using Datatable, datasets and datagrids but now i want to make the same using Generic Collections, so can anybody help me?

I want to have complete converted code for my code....
Following is mine code for EmployeeMaster...

Form1.cs


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;
namespace EmployeeMaster
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }        
        DataTable dt1;        
        string EmpID = "";
        SqlDataAdapter da = new SqlDataAdapter();
        SqlConnection con = new SqlConnection();
        string DepartmentID = "";        
        private void btnNewRecord_Click(object sender, EventArgs e)
        {
            comboBox1.SelectedIndex = 0;
            comboBox2.SelectedIndex = 0;
            textBox1.Clear();
            textBox1.Visible = false;
            label2.Visible = false;
            textBox2.Clear();
            textBox3.Clear();
            textBox4.Clear();            
            textBox6.Clear();
            textBox7.Clear();
            textBox8.Clear();            
            textBox2.ReadOnly = false;
            textBox2.Enabled = true;
            date1.Enabled = true;
            textBox6.ReadOnly = false;
            textBox7.ReadOnly = false;
            textBox8.ReadOnly = false;
            textBox6.Enabled = true;
            textBox7.Enabled = true;
            textBox8.Enabled = true;
            errorProvider1.Dispose();
            btnUpdate.Enabled = true;
            date1.Format = DateTimePickerFormat.Custom;            
            date1.Value = DateTime.Now;
            textBox2.Focus();
            if (btnUpdate.Text == "Update")
            {
                btnUpdate.Text = "Save"; 
            }            
        }
        private void run()
        {
            dt1 = new DataTable();
            EmpMaster get = new EmpMaster();
            dt1= get.Get();
            dataGridView1.DataSource = dt1;                       
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            textBox1.Visible = false;
            label2.Visible = false;
            run();
            EmpMaster getemptype = new EmpMaster();
            dt1 = getemptype.GetEmpType();
            comboBox1.DisplayMember = "EmploymentType";
            comboBox1.ValueMember = "EmpType_ID";
            comboBox1.DataSource = dt1;            

            EmpMaster getdept = new EmpMaster();
            dt1 = getdept.GetDept();
            comboBox2.DisplayMember = "departmentname";
            comboBox2.ValueMember = "departmentid";
            comboBox2.DataSource = dt1;
            btnUpdate.Enabled = false;
            comboBox1.SelectedIndex = 0;
            comboBox2.SelectedIndex = 0;
            textBox3.Clear();
            textBox4.Clear();
            textBox2.Focus();
        }
        private void dataGridView1_RowHeaderMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            textBox1.Visible = true;
            label2.Visible = true;
            textBox2.Enabled = true;
            btnUpdate.Enabled = true;
            int xyz = e.RowIndex;
            DataRowView dr = (DataRowView)dataGridView1.Rows[xyz].DataBoundItem;
            EmpID = dr["EmpID"].ToString();
            DepartmentID = dr["DepartmentID"].ToString();
            textBox1.Text = dr["EmpID"].ToString();
            textBox2.Text = dr["EmpName"].ToString();            
            comboBox1.SelectedValue = dr["EmpType_ID"].ToString();
            comboBox2.SelectedValue = dr["DepartmentID"].ToString();
            date1.Format = DateTimePickerFormat.Short;
            date1.Value = Convert.ToDateTime( dr["DateofJoining"]);
            textBox6.Text = dr["GrossSalary"].ToString();
            textBox7.Text = dr["NetSalary"].ToString();
            textBox8.Text = dr["Phone"].ToString();            
            btnUpdate.Text = "Update";
            textBox2.ReadOnly = false;
            date1.Enabled = true;
            textBox6.Enabled = true;
            textBox7.Enabled = true;
            textBox8.Enabled = true;
            textBox6.ReadOnly = false;
            textBox7.ReadOnly = false;
            textBox8.ReadOnly = false;
        }
        private void btnUpdate_Click(object sender, EventArgs e)
        {
            textBox2.Enabled = true;
            textBox6.Enabled = true;
            textBox7.Enabled = true;
            textBox8.Enabled = true;
            if (textBox2.Text == "")
            {
                errorProvider1.SetError(textBox2, "You need to enter a valid Name");
                MessageBox.Show("Please fill in the empty fields. All fields are mandatory.", "Employee Master", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            else
            {
                errorProvider1.Dispose();
            }
            if (date1.Value == Convert.ToDateTime("1/1/1753") || date1.Value > Convert.ToDateTime(DateTime.Now))
            {
                errorProvider1.SetError(date1, "Please enter valid date");
                MessageBox.Show("Please enter valid date.", "Employee Master", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            else
            {
                errorProvider1.Dispose();
            }
            if (textBox6.Text == "")
            {
                errorProvider1.SetError(textBox6, "You need to enter a value (Your Gross Salary)");
                MessageBox.Show("Please fill in the empty fields. All fields are mandatory.", "Employee Master", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            else
            {
                errorProvider1.Dispose();
            }
                if (textBox7.Text == "")
                { 
                errorProvider1.SetError(textBox7, "You need to enter a value (Your Net Salary)");
                MessageBox.Show("Please fill in the empty fields. All fields are mandatory.", "Employee Master", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
                }
                else
                {
                errorProvider1.Dispose();
                }                
                if (comboBox1.SelectedIndex == 0)
                {
                    errorProvider1.SetError(comboBox1, "Please enter valid Employment Type");
                    MessageBox.Show("Please mention your EmploymentType - Permanent, Probation, Contractual or Trainee", "Employee Master", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                else
                {
                    errorProvider1.Dispose();
                }
                if (comboBox2.SelectedIndex == 0)
                {
                    errorProvider1.SetError(comboBox2, "Please enter valid Department Type");
                    MessageBox.Show("Please mention your EmploymentType - Permanent, Probation, Contractual or Trainee", "Employee Master", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                else
                {
                    errorProvider1.Dispose();
                }
                if (date1.Value < DateTime.Now)
                {
                    errorProvider1.SetError(date1, "Please enter valid Date");
                    MessageBox.Show("Please mention your DOJ", "Employee Master", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
            EmpMaster updatedb = new EmpMaster();
            updatedb.EmpID = EmpID;
            updatedb.EmpName = textBox2.Text;
            updatedb.EmpType_ID = comboBox1.SelectedValue.ToString();
            updatedb.DepartmentID = comboBox2.SelectedValue.ToString();
            updatedb.DateofJoining = date1.Value;
            updatedb.GrossSalary = textBox6.Text;
            updatedb.NetSalary = textBox7.Text;
            updatedb.Phone = textBox8.Text;

            if (btnUpdate.Text == "Update")
            {                   
                if (updatedb.UpdateDB() < 0)
                {
                    MessageBox.Show(updatedb.errortext);
                    return;
                }
                run();
                foreach (DataGridViewRow dr in dataGridView1.Rows)
                {
                    if (dr.Cells["EmpID"].Value.ToString() == EmpID)
                    {
                        dr.Selected = true;
                    }
                }
                dataGridView1.CurrentRow.Selected = false;
                dataGridView1.CurrentCell.Selected = false;  
                MessageBox.Show("Record Updated");
            }
            else if (btnUpdate.Text == "Save")
            {
                if (updatedb.SaveDB() < 0)
                {
                    MessageBox.Show(updatedb.errortext);
                    return;
                }
                run();
                foreach (DataGridViewRow dr in dataGridView1.Rows)
                {
                    if (dr.Cells["EmpID"].Value.ToString() == EmpID)
                    {
                        dr.Selected = true;
                    }
                }
                dataGridView1.CurrentRow.Selected = false;
                dataGridView1.CurrentCell.Selected = false;  
                MessageBox.Show("Record Saved");
            }                     
        }
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBox1.SelectedIndex == 0)
            {
                textBox3.Clear();
                textBox4.Clear();
            }
            else
            {
                textBox3.Text = comboBox1.SelectedValue.ToString();
            }
        }
        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBox1.SelectedIndex == 0)
            {
                textBox3.Clear();
                textBox4.Clear();
            }
            else
            {
                textBox4.Text = comboBox2.SelectedValue.ToString();
            }
        }

        private void date1_DropDown(object sender, EventArgs e)
        {
            date1.Format = DateTimePickerFormat.Short;
            date1.Value = DateTime.Now;            
        }

        private void textBox8_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!((e.KeyChar > 47 && e.KeyChar <= 57) || (e.KeyChar == 8) || (e.KeyChar == 13) || (e.KeyChar == 9) || (e.KeyChar == 0) || (e.KeyChar == 127)))
            {
                errorProvider1.SetError(textBox8, "Please use 0-9 keys only. (Numpad)");
                MessageBox.Show("Please enter 0-9. Phone numbers don't have alphabets.", "Employee Master", MessageBoxButtons.OK, MessageBoxIcon.Information);
                e.Handled = true;
            }
        }

        private void textBox6_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!((e.KeyChar > 47 && e.KeyChar <= 57) || (e.KeyChar == 8) || (e.KeyChar == 13) || (e.KeyChar == 9) || (e.KeyChar == 0) || (e.KeyChar == 127)))
            {
                errorProvider1.SetError(textBox6, "Please use 0-9 keys only. (Numpad)");
                MessageBox.Show("Please enter 0-9. Salary don't have alphabets.", "Employee Master", MessageBoxButtons.OK, MessageBoxIcon.Information);
                e.Handled = true;
            }
        }

        private void textBox7_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!((e.KeyChar > 47 && e.KeyChar <= 57) || (e.KeyChar == 8) || (e.KeyChar == 13) || (e.KeyChar == 9) || (e.KeyChar == 0) || (e.KeyChar == 127)))
            {
                errorProvider1.SetError(textBox7, "Please use 0-9 keys only. (Numpad)");
                MessageBox.Show("Please enter 0-9. Salary don't have alphabets.", "Employee Master", MessageBoxButtons.OK, MessageBoxIcon.Information);
                e.Handled = true;
            }
        }
    }
}


EmpMaster.cs


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace EmployeeMaster
{    
   public class EmpMaster
    {
            DataSet ds;
            DataSet dsdept;
            DataSet dsemptype;            
            SqlDataAdapter da;            
            SqlCommand sc1;       
            public string EmpID
            {
                get;
                set;
            }
            public string EmpName
            {
                get;
                set;
            }
            public string EmpType_ID
            {
                get;
                set;
            }
            public string DepartmentID
            {
                get;
                set;
            }
            public string GrossSalary
            {
                get;
                set;
            }
            public string NetSalary
            {
                get;
                set;
            }
            public string Phone
            {
                get;
                set;
            }
            public DateTime DateofJoining
            {
                get;
                set;
            }
            public string errortext
            {
                get;
                set;
            }
        public DataTable Get()
            {                
                ds = new DataSet();                
                SqlDataAdapter da = new SqlDataAdapter();
                SqlConnection con = new SqlConnection();
                SqlCommand sc1 = new SqlCommand();
                sc1.CommandText = "Select EmployeeMaster.*, d.DepartmentName, z.EmploymentType from EmployeeMaster inner join dbo.Department d on d.DepartmentID = EmployeeMaster.DepartmentID inner join dbo.EmpType z on z.EmpType_ID = EmployeeMaster.EmpType_ID";
                da.SelectCommand = sc1;
                sc1.Connection = con;
                con.ConnectionString = ConfigurationSettings.AppSettings["con"];
                con.Open();
                da.Fill(ds);
                con.Close();            
                return ds.Tables[0];            
        }
        public DataTable GetEmpType()
        {
            SqlConnection con = new SqlConnection();
            sc1 = new SqlCommand();
            da = new SqlDataAdapter();
            sc1.CommandText = "Select * from EmpType order by EmploymentType";
            da.SelectCommand = sc1;
            sc1.Connection = con;
            con.ConnectionString = ConfigurationSettings.AppSettings["con"];
            con.Open();
            dsdept = new DataSet();
            da.Fill(dsdept);
            con.Close();
            return dsdept.Tables[0];
        }
        public DataTable GetDept()
        {
            SqlConnection con = new SqlConnection();
            sc1 = new SqlCommand();
            da = new SqlDataAdapter();
            sc1.CommandText = "Select * from Department order by DepartmentName";
            da.SelectCommand = sc1;
            sc1.Connection = con;
            con.ConnectionString = ConfigurationSettings.AppSettings["con"];
            con.Open();
            dsemptype = new DataSet();
            da.Fill(dsemptype);
            con.Close();
            return dsemptype.Tables[0];
        }
        public int UpdateDB()
        {
            int abc=0;
            SqlConnection con = new SqlConnection();
            try
            {

                SqlCommand sc1 = new SqlCommand();
                sc1.CommandText = "Update Employeemaster set phone ='" + Phone + "', empname ='" + EmpName + "', dateofjoining = '" + DateofJoining.ToShortDateString() + "', grosssalary = '" + GrossSalary + "', netsalary = '" + NetSalary + "', EmpType_ID = '" + EmpType_ID + "', DepartmentID = '" + DepartmentID + "' where EmpID = '" + EmpID + "'";
                SqlDataAdapter da = new SqlDataAdapter();
                da.SelectCommand = sc1;
                sc1.Connection = con;
                con.ConnectionString = ConfigurationSettings.AppSettings["con"];
                con.Open();
                abc = sc1.ExecuteNonQuery();
                if (abc < 0)
                {
                    return -1;
                }
               
            }
            catch (Exception ex)
            {
                errortext = ex.Message;
                return -1;
            }
            finally
            {
                con.Close();
            }
            return abc;
        }
        public int SaveDB()
        {
            int def = 0;
            SqlConnection con = new SqlConnection();
            try
            {
                SqlCommand sc1 = new SqlCommand();
                sc1.CommandText = "Insert into Employeemaster (empname, EmpType_ID, DepartmentID, grosssalary, netsalary, phone, dateofjoining) values ('" + EmpName + "', '" + EmpType_ID + "', '" + DepartmentID + "', '" + GrossSalary + "', '" + NetSalary + "', '" + Phone + "', '" + Convert.ToDateTime(DateofJoining).ToShortDateString() + "')";
                SqlDataAdapter da = new SqlDataAdapter();
                da.SelectCommand = sc1;
                sc1.Connection = con;
                con.ConnectionString = ConfigurationSettings.AppSettings["con"];
                con.Open();
                def = sc1.ExecuteNonQuery();
                if (def < 0)
                {
                    return -1;
                }
            }
            catch (Exception ex)
            {
                errortext = ex.Message;
                return -1;
            }
            finally
            {
                con.Close();
            }
            return def;
        }
    }
}

推荐答案


foreach row in datatable
   1. create a new employee object

   foreach column in row
      1. Using reflection set the property name with the column value

   2. add the created employee to generic collection


这篇关于使用通用集合的EmployeeMaster的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-22 05:09