c#应用程序没有响应我的datagridview cellendedit事件?

时间:2017-11-11 05:01:22

标签: c# winforms ms-access datagridview

朋友,我在c#中创建了一个工资单应用程序。 我使用计算4列的cellendedit事件创建了用于批量记录计算的datagridview。但是10-20记录它的工作正常,当记录增加1000个应用程序在单元格中输入值后没有响应请帮助我发送我的代码和我的datagridview的屏幕截图 Before entering value in Working_Days Column After entering value in Working_Days

using System;
using System.Data;
using System.Data.OleDb;
using System.Globalization;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
public partial class MonthDeduction : Form
    {
    Database database = new Database();
    int indexofrow;
    OleDbDataAdapter sda;
    OleDbDataReader sdr; OleDbCommand scm; BindingSource bs;
    string query;
    double tmp, monthdays, totaltds,NetSalary;

    private OleDbDataReader SelectMonthValue()
        {
        query = "Select Top 1000 * from " + comboBox1.SelectedItem.ToString() + "";
        scm = new OleDbCommand(query,database.myConnection);
        try
            {
            database.OpenConnection();
            sdr = scm.ExecuteReader();
            return sdr;
            }
        catch (Exception)
            {
            throw;
            }
        }

    private void dataGridView1_CellEndEdit_1(object sender,DataGridViewCellEventArgs e)
        {
        indexofrow = e.RowIndex;
        if (comboBox1.SelectedText != null)
            {
            int month = DateTime.ParseExact(Convert.ToString(comboBox1.SelectedItem),"MMMM",CultureInfo.InvariantCulture).Month;
            monthdays = DateTime.DaysInMonth(DateTime.Now.Year,month);
            }
        if (dataGridView1.Rows != null)
            {
            DataGridViewRow newDataRow = dataGridView1.Rows[indexofrow];
            double wd = Convert.ToDouble(newDataRow.Cells["Working_Days"].Value);
            double epf = Convert.ToDouble(newDataRow.Cells["EPF"].Value);
            double tds = Convert.ToDouble(newDataRow.Cells["TDS"].Value);
            double salary = Convert.ToDouble(newDataRow.Cells["Basic_Salary"].Value);
            DateTime dob = Convert.ToDateTime(newDataRow.Cells["DOB"].Value);
            double yearlysalary = salary * 12;
            totaltds = TotalTds();
            double twofifty = 250000 / 12;
            double three = 300000 / 12;
            double five = 500000 / 12;
            double ten = 1000000 / 12;
            double finalsalary = (salary / monthdays) * wd;
            int age = 0;
            age = DateTime.Now.Year - dob.Year;
            if (DateTime.Now.DayOfYear < dob.DayOfYear)
                {
                age = age - 1;
                }
            if (wd > monthdays)
                {
                DialogResult result = MessageBox.Show("Working Days Could Not Be Greater Than MonthDays","Warning",MessageBoxButtons.OK,MessageBoxIcon.Warning);
                if (result == System.Windows.Forms.DialogResult.OK)
                    {
                    newDataRow.Cells["Working_Days"].Value = 0;
                    newDataRow.Cells["EPF"].Value = 0;
                    newDataRow.Cells["TDS"].Value = 0;
                    newDataRow.Cells["Final_Amt"].Value = 0;
                    }
                }
            else if (salary <= 15000)
                {
                epf = finalsalary * .12;
                tds = 0;
                NetSalary = Convert.ToInt32(finalsalary - epf);
                }
            else if (salary > 15000 & salary < twofifty)
                {
                tds = 0;
                epf = 0;
                NetSalary = Convert.ToInt32(finalsalary);
                }
            else if (age < 60 & salary > twofifty & salary < five)
                {
                tmp = (yearlysalary - 250000) * .05;
                tds = ((tmp + (tmp * .02)) / 12) - totaltds;
                epf = 0;
                NetSalary = Convert.ToInt32(finalsalary - tds);
                }
            else if (age < 60 & salary > five & salary < ten)
                {
                tmp = 12500 + ((yearlysalary - 500000) * .20);
                tds = ((tmp + (tmp * .02)) / 12) - totaltds;
                epf = 0;
                NetSalary = Convert.ToInt32(finalsalary - tds);
                }
            else if (age < 60 & salary > ten)
                {
                tmp = 112500 + ((yearlysalary - 1000000) * .30);
                tds = ((tmp + (tmp * .02)) / 12) - totaltds;
                epf = 0;
                NetSalary = Convert.ToInt32(finalsalary - tds);
                }

            else if (age > 60 & age < 80 & salary > three & salary < five)
                {
                tmp = yearlysalary * .05;
                tds = ((tmp + (tmp * .02)) / 12) - totaltds;
                epf = 0;
                NetSalary = Convert.ToInt32(finalsalary - tds);
                }
            else if (age > 60 & age < 80 & salary > five & salary < ten)
                {
                tmp = 10000 + ((yearlysalary - 500000) * .20);
                tds = ((tmp + (tmp * .02)) / 12) - totaltds;
                epf = 0;
                NetSalary = Convert.ToInt32(finalsalary - tds);
                }
            else if (age < 80 & age > 60 & salary > ten)
                {
                tmp = 110000 + ((yearlysalary - 1000000) * .30);
                tds = ((tmp + (tmp * .02)) / 12) - totaltds;
                epf = 0;
                NetSalary = Convert.ToInt32(finalsalary - tds);
                }
            else if (age > 80 & salary > five & salary < ten)
                {
                tmp = yearlysalary * .20;
                tds = ((tmp + (tmp * .02)) / 12) - totaltds;
                epf = 0;
                NetSalary = Convert.ToInt32(finalsalary - tds);
                }
            else if (age > 80 & salary > ten)
                {
                tmp = 100000 + ((yearlysalary - 1000000) * .30);
                tds = ((tmp + (tmp * .02)) / 12) - totaltds;
                epf = 0;
                NetSalary = Convert.ToInt32(finalsalary - tds);
                }
            newDataRow.Cells["EPF"].Value = Convert.ToInt32(epf);
            newDataRow.Cells["TDS"].Value = Convert.ToInt32(tds);
            newDataRow.Cells["Final_Amt"].Value = NetSalary;
            }
        }

    private void advancedDataGridView1_SortStringChanged(object sender,EventArgs e)
        {
        this.bs.Sort = this.dataGridView1.SortString;
        }

    private void advancedDataGridView1_FilterStringChanged(object sender,EventArgs e)
        {
        this.bs.Filter = this.dataGridView1.FilterString;
        }
    private void dataGridView1_CellEndEdit(object sender,DataGridViewCellEventArgs e)
        {
        }
    public MonthDeduction()
        {
        InitializeComponent();
        }
    private double TotalTds()
        {
        if (indexofrow >= 0)
            {
            foreach (DataGridViewRow row in dataGridView1.Rows)
                {
                if (dataGridView1.Rows != null)
                    {
                    try
                        {
                        database.OpenConnection();
                        query = "SELECT TotalTDS FROM AnnualTDS where Employee_Code=@a";
                        scm = new OleDbCommand(query,database.myConnection);
                        scm.Parameters.AddWithValue("@a","" + row.Cells["Employee_Code"].Value + "");
                        sdr = scm.ExecuteReader();
                        while (sdr.Read())
                            {
                            totaltds = Convert.ToDouble(sdr.GetValue(0));
                            }
                        database.CloseConnection();
                        }
                    catch (Exception)
                        {
                        throw;
                        }
                    }
                }
            }
        return totaltds;
        }

    private void button2_Click(object sender,EventArgs e)
        {
        sdr = SelectMonthValue();
        if (sdr.Read() == false)
            {                           
            foreach (DataGridViewRow row in dataGridView1.Rows)
                {
                using (scm = new OleDbCommand("INSERT INTO " + comboBox1.SelectedItem.ToString() + " (Employee_Code,Working_Days,EPF,TDS,Final_Amt) VALUES(@b,@c,@d,@e,@f)",database.myConnection))
                    {
                    try
                        {                                                                  
                        scm.Parameters.AddWithValue("@b",row.Cells["Employee_Code"].Value);
                        scm.Parameters.AddWithValue("@c",row.Cells["Working_Days"].Value);
                        scm.Parameters.AddWithValue("@d",row.Cells["EPF"].Value);
                        scm.Parameters.AddWithValue("@e",row.Cells["TDS"].Value);
                        scm.Parameters.AddWithValue("@f",row.Cells["Final_Amt"].Value);
                        scm.ExecuteNonQuery();                                             
                        }
                    catch (Exception ex)
                        {
                        MessageBox.Show(ex.ToString(),"Message",MessageBoxButtons.OK,MessageBoxIcon.Information);
                        }
                    }
                }
            }
        else
            {
            foreach (DataGridViewRow row in dataGridView1.Rows)
                {
                using (scm = new OleDbCommand("UPDATE " + comboBox1.SelectedItem.ToString() + " SET Working_Days=@c,EPF=@d,TDS=@e,Final_Amt=@f where Employee_Code=@b",database.myConnection))
                    {
                    try
                        {                                                                  
                        scm.Parameters.AddWithValue("@c",row.Cells["Working_Days"].Value);
                        scm.Parameters.AddWithValue("@d",row.Cells["EPF"].Value);
                        scm.Parameters.AddWithValue("@e",row.Cells["TDS"].Value);
                        scm.Parameters.AddWithValue("@f",row.Cells["Final_Amt"].Value);
                        scm.Parameters.AddWithValue("@b",row.Cells["Employee_Code"].Value);
                        scm.ExecuteNonQuery();                                            
                        }
                    catch (Exception ex)
                        {
                        MessageBox.Show(ex.ToString(),"Message",MessageBoxButtons.OK,MessageBoxIcon.Information);
                        }
                    }
                }
            }
        if (comboBox1.SelectedIndex == 0)
            {
            query = "Update Annual_Data SET [Working_Days(Apr)]=@a,[EPF(Apr)]=@b,[TDS(Apr)]=@c,[NetPay(Apr)]=@d where Employee_Code = @e";
            }
        else if (comboBox1.SelectedIndex == 1)
            {
            query = "Update Annual_Data SET [Working_Days(May)]=@a,[EPF(May)]=@b,[TDS(May)]=@c,[NetPay(May)]=@d where Employee_Code = @e";
            }
        else if (comboBox1.SelectedIndex == 2)
            {
            query = "Update Annual_Data SET [Working_Days(Jun)]=@a,[EPF(Jun)]=@b,[TDS(Jun)]=@c,[NetPay(Jun)]=@d where Employee_Code = @e";
            }
        else if (comboBox1.SelectedIndex == 3)
            {
            query = "Update Annual_Data SET [Working_Days(Jul)]=@a,[EPF(Jul)]=@b,[TDS(Jul)]=@c,[NetPay(Jul)]=@d where Employee_Code = @e";
            }
        else if (comboBox1.SelectedIndex == 4)
            {
            query = "Update Annual_Data SET [Working_Days(Aug)]=@a,[EPF(Aug)]=@b,[TDS(Aug)]=@c,[NetPay(Aug)]=@d where Employee_Code = @e";
            }
        else if (comboBox1.SelectedIndex == 5)
            {
            query = "Update Annual_Data SET [Working_Days(Sep)]=@a,[EPF(Sep)]=@b,[TDS(Sep)]=@c,[NetPay(Sep)]=@d where Employee_Code = @e";
            }
        else if (comboBox1.SelectedIndex == 6)
            {
            query = "Update Annual_Data SET [Working_Days(Oct)]=@a,[EPF(Oct)]=@b,[TDS(Oct)]=@c,[NetPay(Oct)]=@d where Employee_Code = @e";
            }
        else if (comboBox1.SelectedIndex == 7)
            {
            query = "Update Annual_Data SET [Working_Days(Nov)]=@a,[EPF(Nov)]=@b,[TDS(Nov)]=@c,[NetPay(Nov)]=@d where Employee_Code = @e";
            }
        else if (comboBox1.SelectedIndex == 8)
            {
            query = "Update Annual_Data SET [Working_Days(Dec)]=@a,[EPF(Dec)]=@b,[TDS(Dec)]=@c,[NetPay(Dec)]=@d where Employee_Code = @e";
            }
        else if (comboBox1.SelectedIndex == 9)
            {
            query = "Update Annual_Data SET [Working_Days(Jan)]=@a,[EPF(Jan)]=@b,[TDS(Jan)]=@c,[NetPay(Jan)]=@d where Employee_Code = @e";
            }
        else if (comboBox1.SelectedIndex == 10)
            {
            query = "Update Annual_Data SET [Working_Days(Feb)]=@a,[EPF(Feb)]=@b,[TDS(Feb)]=@c,[NetPay(Feb)]=@d where Employee_Code = @e";
            }
        else if (comboBox1.SelectedIndex == 11)
            {
            query = "Update Annual_Data SET [Working_Days(Mar)]=@a,[EPF(Mar)]=@b,[TDS(Mar)]=@c,[NetPay(Mar)]=@d where Employee_Code = @e";
            }
        foreach (DataGridViewRow row in dataGridView1.Rows)
            {
            try
                {
                scm = new OleDbCommand(query,database.myConnection);
                scm.Parameters.AddWithValue("@a",row.Cells["Working_Days"].Value);
                scm.Parameters.AddWithValue("@b",row.Cells["EPF"].Value);
                scm.Parameters.AddWithValue("@c",row.Cells["TDS"].Value);
                scm.Parameters.AddWithValue("@d",row.Cells["Final_Amt"].Value);
                scm.Parameters.AddWithValue("@e",row.Cells["Employee_Code"].Value);
                scm.ExecuteNonQuery();
                }
            catch (Exception ex)
                {
                MessageBox.Show(ex.ToString());
                }
            }
        MessageBox.Show("Data Saved","Done",MessageBoxButtons.OK,MessageBoxIcon.Information);
        database.CloseConnection();
        }
    private void comboBox1_SelectedIndexChanged(object sender,EventArgs e)
        {
        TotalTds();
        if (comboBox1.SelectedItem != null)
            {
            sdr = SelectMonthValue();
            if (sdr.Read() == false)
                {
                query = "Select Top 1000 Employee_Code,Name,Father_Name,DOB,Place_Of_Post,Program_Name,Designation,Date_Hired,Bank_Name,IFSC,Bank_Ac,Basic_Salary,Working_Days,EPF,TDS,Final_Amt from empinfo";
                scm = new OleDbCommand(query,database.myConnection);
                scm.ExecuteNonQuery();
                sda = new OleDbDataAdapter(scm);
                }
            else
                {
                if (comboBox1.SelectedIndex == 0)
                    {
                    query = "select Top 1000 * from Apr";
                    }
                else if (comboBox1.SelectedIndex == 1)
                    {
                    query = "select Top 1000 * from Mai";
                    }
                else if (comboBox1.SelectedIndex == 2)
                    {
                    query = "select Top 1000 * from Jun";
                    }
                else if (comboBox1.SelectedIndex == 3)
                    {
                    query = "select Top 1000 * from Jul";
                    }
                else if (comboBox1.SelectedIndex == 4)
                    {
                    query = "select Top 1000 * from Aug";
                    }
                else if (comboBox1.SelectedIndex == 5)
                    {
                    query = "select Top 1000 * from Sep";
                    }
                else if (comboBox1.SelectedIndex == 6)
                    {
                    query = "select Top 1000 * from Oct";
                    }
                else if (comboBox1.SelectedIndex == 7)
                    {
                    query = "select Top 1000 * from Nov";
                    }
                else if (comboBox1.SelectedIndex == 8)
                    {
                    query = "select Top 1000 * from Dec";
                    }
                else if (comboBox1.SelectedIndex == 9)
                    {
                    query = "select Top 1000 * from Jan";
                    }
                else if (comboBox1.SelectedIndex == 10)
                    {
                    query = "select Top 1000 * from Feb";
                    }
                else if (comboBox1.SelectedIndex == 11)
                    {
                    query = "select Top 1000 * from Mar";
                    }
                scm = new OleDbCommand(query,database.myConnection);
                scm.ExecuteNonQuery();
                sda = new OleDbDataAdapter(scm);
                }
            DataTable dt = new DataTable();
            sda.Fill(dt);
            bs = new BindingSource();
            bs.DataSource = dt.DefaultView;
            dataGridView1.DataSource = bs;
            dataGridView1.AutoResizeColumns();
            dataGridView1.Columns["Employee_Code"].Frozen = true;
            dataGridView1.Sort(dataGridView1.Columns["Employee_Code"],System.ComponentModel.ListSortDirection.Ascending);
            dataGridView1.Columns["Name"].Frozen = true;
            dataGridView1.Columns["Father_Name"].Frozen = true;
            dataGridView1.Columns["Employee_Code"].ReadOnly = true;
            dataGridView1.Columns["Name"].ReadOnly = true;
            dataGridView1.Columns["Father_Name"].ReadOnly = true;
            dataGridView1.Columns["DOB"].ReadOnly = true;
            dataGridView1.Columns["Place_Of_Post"].ReadOnly = true;
            dataGridView1.Columns["Program_Name"].ReadOnly = true;
            dataGridView1.Columns["Designation"].ReadOnly = true;
            dataGridView1.Columns["Date_Hired"].ReadOnly = true;
            dataGridView1.Columns["Bank_Name"].ReadOnly = true;
            dataGridView1.Columns["IFSC"].ReadOnly = true;
            dataGridView1.Columns["Bank_Ac"].ReadOnly = true;
            dataGridView1.Columns["Basic_Salary"].ReadOnly = true;
            dataGridView1.Columns["EPF"].ReadOnly = true;
            dataGridView1.Columns["TDS"].ReadOnly = true;
            dataGridView1.Columns["Final_Amt"].ReadOnly = true;
            label2.Text = string.Format("Total Employee : {0}",dataGridView1.RowCount);
            if (dataGridView1.Rows.Count > 0)
                {
                dataGridView1.CurrentCell = dataGridView1.Rows[0].Cells["Working_Days"];
                dataGridView1.BeginEdit(true);
                }
            sda.Update(dt);
            database.CloseConnection();
            }
        else
            {
            MessageBox.Show("Please Select Month");
            }
        }
    }
}

0 个答案:

没有答案