Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# DataGridView的加载速度非常慢。如何优化DataGridView中的行添加?_C#_.net_Database_Datagridview - Fatal编程技术网

C# DataGridView的加载速度非常慢。如何优化DataGridView中的行添加?

C# DataGridView的加载速度非常慢。如何优化DataGridView中的行添加?,c#,.net,database,datagridview,C#,.net,Database,Datagridview,datagridview加载速度非常慢。如何优化它? datagridview有4000-5000行。 我必须在几个参数上动态生成datagridview。(来自数据库的数据,列数) 我必须从数据库中的表(id、名称、联系人)垂直生成datagridview 第1列 id 名称 联系方式 现在,在列1旁边可以有任意数量的空列。 目前我正在采用这种方法 首先添加所有空列 然后在每个循环迭代中添加三行,每个循环迭代添加一行(id、姓名、联系人) 我正在从数据库获取数据,并将其作为列表传递给Gener

datagridview加载速度非常慢。如何优化它?

datagridview有4000-5000行。
我必须在几个参数上动态生成datagridview。(来自数据库的数据,列数)

我必须从数据库中的表(id、名称、联系人)垂直生成datagridview

第1列

id
名称
联系方式

现在,在列1旁边可以有任意数量的空列。

目前我正在采用这种方法

  • 首先添加所有空列
  • 然后在每个循环迭代中添加三行,每个循环迭代添加一行(id、姓名、联系人)
  • 我正在从数据库获取数据,并将其作为
    列表
    传递给
    GeneratorOWS
    函数

     private void GenerateColumns(int colLen)
        {
            dataGridViewGenerate.Rows.Clear();
            dataGridViewGenerate.Columns.Clear();
    
            DataGridViewColumn col0 = new DataGridViewTextBoxColumn();
            col0.HeaderText = "Employee No. & Name";
            dataGridViewGenerate.Columns.Add(col0);
    
            for (int i = 0; i < colLen; i++)
            {
                DataGridViewColumn col = new DataGridViewTextBoxColumn
                    {
                        HeaderText =
                            (_sTime.AddDays(i)).Day.ToString(CultureInfo.InvariantCulture) + " " +
                            (_sTime.AddDays(i)).ToString("ddd")
                    };
    
                dataGridViewGenerate.Columns.Add(col);
        }
    
    
    private void GenerateRows(List<string[]> empList)
        {
            int len = empList.Count;
            for (int a = 0; a < len; a++)
            {
                string[] arr = empList[a];
                //row 1
                var row1 = new DataGridViewRow();
                row1.Cells.Add(new DataGridViewTextBoxCell
                    {
                        Value = arr[0]
                    });
                dataGridViewGenerate.Rows.Add(row1);
    
                //row 2
                var row2 = new DataGridViewRow();
                row2.Cells.Add(new DataGridViewTextBoxCell
                    {
                        Value = arr[1]
                    });
                dataGridViewGenerate.Rows.Add(row2);
    
                //row3
    
                var row3 = new DataGridViewRow();
                row3.Cells.Add(new DataGridViewTextBoxCell
                    {
                        Value = arr[2]
                    });
                dataGridViewGenerate.Rows.Add(row3);
            }
        }
    
    private void GenerateColumns(int colLen)
    {
    dataGridViewGenerate.Rows.Clear();
    dataGridViewGenerate.Columns.Clear();
    DataGridViewColumn col0=新的DataGridViewTextBoxColumn();
    col0.HeaderText=“员工编号和姓名”;
    dataGridViewGenerate.Columns.Add(col0);
    对于(int i=0;i
  • 我正在考虑用sql创建一个过程,该过程将创建一个表并用数据填充它。然后只需将数据源分配给datagridview。

    调用Rows.Add()非常昂贵。这是一个快速而肮脏的改变,可以解决这个问题

    private void GenerateRows(List<string[]> empList)
    {
        List<DataGridViewRow> rows = new List<DataGridViewRow>();
        int len = empList.Count;
        for (int a = 0; a < len; a++)
        {
            string[] arr = empList[a];
            //row 1
            var row1 = new DataGridViewRow();
            row1.Cells.Add(new DataGridViewTextBoxCell{Value = arr[0]});
            /* CHANGED to add to List */
            rows.Add(row1);
    
            //row 2
            var row2 = new DataGridViewRow();
            row2.Cells.Add(new DataGridViewTextBoxCell{Value = arr[1]});
            /* CHANGED to add to List */
            rows.Add(row2);
    
            //row3
    
            var row3 = new DataGridViewRow();
            row3.Cells.Add(new DataGridViewTextBoxCell{Value = arr[2]});
            /* CHANGED to add to List */
            rows.Add(row3);
        }
    
        /* ADDED all rows at once for performance */
        dataGridViewGenerate.Rows.AddRange(rows.ToArray());
    }
    
    private void GenerateRows(列表列表列表)
    {
    列表行=新列表();
    int len=empList.Count;
    对于(int a=0;a
    问题在于,每当向Datagridview添加行时,它都会重新绘制, 如果您不介意使用P/Invoke,可以尝试此解决方案 //挂漆

    SendMessage(Datagridview.Handle, WM_SETREDRAW, false, 0);
    
    //您的循环将在此处添加行

    然后调用下面的方法继续绘制

    SendMessage(Datagridview.Handle, WM_SETREDRAW, true, 0);
    
    p/Invoke声明应该是这样的

    [System.Runtime.InteropServices.DllImport("user32.dll")]
    public static extern int SendMessage(IntPtr hWnd, Int32 wMsg, bool wParam, Int32 lParam);
    private const int WM_SETREDRAW = 0x000B;
    

    我所做的不是一行一行地添加到
    DataGridView
    。我只是从我的数据创建了一个
    DataTable
    ,并将其作为
    数据源分配给
    DataGridView


    加载
    DataGridView
    的性能显著提高。

    为什么要加载4000-5000行?用户实际检查4000-5000行的频率有多高。如果有三列,并且可以在1秒内读取每个单元格,则仍需要3个多小时才能读取。一次读取100或1000行。我必须创建这种类型的datagrid因为数据输入工作将从这个网格中完成。对于数据输入操作员来说,这将是一项非常繁琐的任务。实际上,4000行datagridview是您输入数据的唯一选项。对于数据输入操作员来说,加载速度非常慢并不是一项繁琐的任务?只有他们手中的报告中的数据才会被填充。我也是这样做的不知道将填充哪些记录。因此我必须显示所有记录。然后将打印完整的数据网格。甚至是空单元格。对于遗漏的手写数据输入,在添加行时暂停绘制或使用addrange有什么更好的方法。或者我应该同时使用这两种方法。即暂停绘制和addrange。这似乎很简单r对我来说,但您可以尝试每种方法,看看您更喜欢什么。您已经指定要手动添加行,因此我假设您不想使用数据绑定。否则这就是我的答案。在数据绑定中有更多优化的方法来挂起数据更新。我在DataTable中有20000行,并尝试分配给的数据源datagridview非常慢,在WM_SETREDRAW之后,我不得不调用datagridview.Refresh(),以使网格看起来完全绘制出来