首页 > 解决方案 > 在 datagridview 中处理数据变得非常缓慢

问题描述

所以我有一个几年前制作的程序,它可以帮助我处理和纠正我的工作数据。我每年运行一次,直到明年才忘记它。

我有一个大数据文件(通常超过 80 万行和 5 列),必须通过各种检查数据的正确性。我通过 excel 电子表格将文件绑定到 datagridview 中,如下所示:

 public DataTable ReadExcel(string fileName, string fileExt)
    {
        string conn = string.Empty;
        DataTable dtexcel = new DataTable();
        if (fileExt.CompareTo(".xlsx") == 0)
        {
            conn = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + fileName + ";Extended Properties='Excel 12.0;HDR=NO';"; //for above excel 2007  
        }

        using (OleDbConnection con = new OleDbConnection(conn))
        {

            try
            {
                OleDbDataAdapter oleAdpt = new OleDbDataAdapter("SELECT * FROM [Sheet1$]", con); //here we read data from sheet1

                oleAdpt.Fill(dtexcel); //fill excel data into dataTable  
            }
            catch { }
        }
        return dtexcel;
    }

我也使用这个加载文件:

private void loadExcelTable_Click(object sender, EventArgs e)
    {
        string filePath = string.Empty;
        string fileExt = string.Empty;
        OpenFileDialog file = new OpenFileDialog(); //open dialog to choose file  
        if (file.ShowDialog() == DialogResult.OK) //if there is a file choosen by the user  
        {
            filePath = file.FileName; //get the path of the file  
            fileExt = System.IO.Path.GetExtension(filePath); //get the file extension  
            textBox1.Text = filePath; //display the path to the file in the loadFile text box
            if (fileExt.CompareTo(".xls") == 0 || fileExt.CompareTo(".xlsx") == 0)
            {
                try
                {
                    this.Cursor = Cursors.WaitCursor;
                    phoneGridView1.VirtualMode = true;
                    phoneGridView1.Visible = true;
                    phoneGridView1.DataSource = ReadExcel(filePath, fileExt);
                    this.Cursor = Cursors.Default;
                    MessageBox.Show("Файлът зареден успешно!", "Success!", MessageBoxButtons.OK); //custom message for successful file loading
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message.ToString());
                }
            }
            else
            {
                MessageBox.Show("Моля изберете .xlsx файл.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Error); //custom messageBox to show error  
            }
        }
    }

加载完所有内容后,我按下按钮,内部开始对数据进行各种检查(涉及很多 ifs)。去年我这样做的时候,一切都很顺利,整个文件的检查时间大约为 3 小时。今年发生了一些变化。我不知道它是否来自一些 VS2019 更新或其他什么,但内部检查花费了更多时间。我让文件坐了整整一个星期,它甚至没有检查一半。

我的问题是:有没有办法让事情像以前一样工作?那一年的一些更新是否改变了 datagridview 处理数据的方式?从上次工作到现在,我没有对代码进行任何更改。唯一改变的是我改变了我的 GPU,但我不相信这会导致性能如此巨大的下降(我升级了,没有降级)。对此问题的任何帮助将不胜感激。

用于数据验证的内部检查示例代码:

private void checkEmail(string email, int i)
    {
        //counts the symbols after the @ symbol
        string afterSymbol = email.Substring(email.LastIndexOf('@') + 1);
        int lenght = afterSymbol.Length;

        if (email.Contains("@") && email.Contains(".") && lenght >= 5)
        {
            phoneGridView1.Rows[i].Cells[4].Value = "01";
        }
        else
        {
            phoneGridView1.Rows[i].Cells[4].Value = "02";
        }
        phoneGridView1.Update();
    }

Button_click 函数:这里调用的所有函数都由各种段组成,如上面检查 datagridview 中数据的代码段

private void checks_Click(object sender, EventArgs e)
    {
        if (phoneGridView1.Rows.Count != 0)
        {
            int maxRows = phoneGridView1.RowCount;
            int maxColumns = phoneGridView1.ColumnCount;
            string fieldValue;
            DataTable Codes1 = Codes(textBox2.Text);
            Codes1.PrimaryKey = new DataColumn[] { Codes1.Columns["Column1"] };
            this.Cursor = Cursors.WaitCursor;
            if (Codes1.Rows.Count >0)
            {                                      
                //Check each row if it contains one of the appointed values
                for (int i = 1; i < maxRows; i++)
                {
                    fieldValue = phoneGridView1.Rows[i].Cells[3].Value.ToString();


                    //Checks if the value in the row is the email
                    if (phoneGridView1.Rows[i].Cells[2].Value.ToString() == "3")
                    {
                        checkEmail(fieldValue, i);
                    }

                    //Checks if the value in the row is GSM number
                    if (phoneGridView1.Rows[i].Cells[2].Value.ToString() == "5")
                    {
                        checkGSM(fieldValue, i);
                    }

                    //Check if the telephone number is correct
                    if (phoneGridView1.Rows[i].Cells[2].Value.ToString() == "1")
                    {
                        checkTelephone(fieldValue, i, Codes1);
                    }

                    //Check if the fax number is correct
                    if (phoneGridView1.Rows[i].Cells[2].Value.ToString() == "2")
                    {
                        checkFax(fieldValue, i, Codes1);
                    }

                    //Check if the web page is correct
                    if (phoneGridView1.Rows[i].Cells[2].Value.ToString() == "6")
                    {
                        checkWeb(fieldValue, i);
                    }

                    if (phoneGridView1.Rows[i].Cells[4].Value.ToString() == string.Empty)
                    {
                        phoneGridView1.Rows[i].Cells[4].Value = "19";
                    }
                }
                this.Cursor = Cursors.Default;
                MessageBox.Show("Успешно проверени данни!", "Success!", MessageBoxButtons.OK);
            }
            else
            {
                MessageBox.Show("Моля въведете файл със кодовете на населените места!", "Error!", MessageBoxButtons.OK);
            }
        }
        else
        {
            MessageBox.Show("Моля въведете файл за проверка!", "Error!", MessageBoxButtons.OK);
        }
    }

标签: c#performancedatagridview

解决方案


推荐阅读