欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C# 解决datagridview控件显示大量数据拖拉卡顿问题

程序员文章站 2022-10-01 19:18:52
问题描述:由于在使用sql查询大量的数据并一次显示到datagridview控件,出现拖拉的时候卡顿。解决方法:1.首先分页。2.其次把显示控件设置双buffer。解决过程如下:1.设置datagri...

问题描述:

由于在使用sql查询大量的数据并一次显示到datagridview控件,出现拖拉的时候卡顿。

解决方法:
1.首先分页。
2.其次把显示控件设置双buffer。

解决过程如下:

1.设置datagridview双buffer代码如下,需要引用反射命名空间

 type dgvtype = this.datagridview1.gettype();
      propertyinfo pi = dgvtype.getproperty("doublebuffered", bindingflags.instance | bindingflags.nonpublic);
      pi.setvalue(this.datagridview1, true, null);

2.自己定义分页操作控件,上一页,下一页,首页,尾页,在加载form的时候添加事件。并为他们写一个操作事件

 private void form1_load(object sender, eventargs e)
    {

      this.btn_endpage.click += ctrl_click;
      this.btn_firstpage.click += ctrl_click;
      this.btn_lastpage.click += ctrl_click;
      this.btn_nextpage.click += ctrl_click;


      //this.setstyle(controlstyles.optimizeddoublebuffer | controlstyles.resizeredraw | controlstyles.allpaintinginwmpaint, true);
      //this.updatestyles();
      
  
    }

    private void ctrl_click(object sender, eventargs e)
    {
      button btn = (button)sender;
      if (btn.text == this.btn_endpage.text)
      {
        if (currentpage == pagecount)
        { return; }
        currentpage = pagecount;
        loadpage();
      }
      else if (btn.text == this.btn_firstpage.text)
      {
        if (currentpage == 1)
        { return; }
        currentpage = 1;
        loadpage();
      }
      else if (btn.text == this.btn_lastpage.text)
      {
        if (currentpage == 1)
        { return; }
        currentpage--;
        loadpage();
      }
      else if (btn.text == this.btn_nextpage.text)
      {
        if (currentpage == pagecount)
        { return; }
        currentpage++;
        loadpage();
      }
      else
      {
        messagebox.show("error");
      }


    }

3.下面代码是分页方法,加载page

 #region datagridview sort

    /// <summary>
    /// 每页记录数
    /// </summary>
    private int pagesize = 50;

    /// <summary>
    /// 总记录数
    /// </summary>
    private int recordcount = 0;

    /// <summary>
    /// 总页数
    /// </summary>
    private int pagecount = 0;

    /// <summary>
    /// 当前页
    /// </summary>
    private int currentpage = 0;


    /// <summary>
    /// 分页的方法
    /// </summary>
    /// <param name="str"></param>
    private void pagesorter()
    {
      foreach (datacolumn col in dt.columns)
      {
        datagridviewtextboxcolumn dgvc = new datagridviewtextboxcolumn();
        dgvc.name = "";
        dgvc.datapropertyname = "";
        dgvc.headertext = col.tostring();
        datagridview1.columns.add(dgvc);
      }
      recordcount = dt.rows.count;   //记录总行数
      pagecount = (recordcount / pagesize);
      if ((recordcount % pagesize) > 0)
      {
        pagecount++;
      }
      //默认第一页
      currentpage = 1;
      loadpage();//调用加载数据的方法
    }


    /// <summary>
    /// loadpage方法
    /// </summary>
    private void loadpage()
    {
      if (currentpage < 1) currentpage = 1;
      if (currentpage > pagecount) currentpage = pagecount;

      int beginrecord;  //开始指针
      int endrecord;   //结束指针
      datatable dttemp;
      dttemp = dt.clone();

      beginrecord = pagesize * (currentpage - 1);
      if (currentpage == 1) beginrecord = 0;
      endrecord = pagesize * currentpage;

      if (currentpage == pagecount) endrecord = recordcount;
      for (int i = beginrecord; i < endrecord; i++)
      {
        dttemp.importrow(dt.rows[i]);
      }

      datagridview1.rows.clear();
      this.lb_currentrow.text = "当前页: " + currentpage.tostring() + " / " + pagecount.tostring();//当前页
      this.lb_totalrows.text = "总行数: " + recordcount.tostring() + " 行";//总记录数
                                      


      把临时table的数据插入到datagridview控件里面。
      for (int i = 0; i < dttemp.rows.count; i++)
      {
        datagridview1.rows.add();
        for (int j = 0; j < dttemp.columns.count; j++)
          datagridview1.rows[i].cells[j].value = dttemp.rows[i][j].tostring();
      }
    }

    #endregion

tips:

文中的“dt”是datatable类型的数据,给位看官可以自己创建,也可以从数据库直接获取测试数据。

extension1:

1.设置datagridview自增序列号

 //填充序列号
    private void datagridview1_rowpostpaint_1(object sender, datagridviewrowpostpainteventargs e)
    {
      rectangle rectangle = new rectangle(e.rowbounds.location.x,
        e.rowbounds.location.y,
        datagridview1.rowheaderswidth - 4,
        e.rowbounds.height);
      textrenderer.drawtext(e.graphics, (e.rowindex + 1).tostring(),
        datagridview1.rowheadersdefaultcellstyle.font,
        rectangle,
        datagridview1.rowheadersdefaultcellstyle.forecolor,
        textformatflags.verticalcenter | textformatflags.right);
    }

extension2:

1.设置datagridview自增序列号

    //用委托更新data,防止卡顿。
    private delegate void updatedatagridview(datatable dt);
    private void updategv(datatable dt)
    {
      if (datagridview1.invokerequired)
      {
       //这里没有看懂,有大神可以解释下嘛
        this.begininvoke(new updatedatagridview(updategv), new object[] { dt });
      }
      else
      {
        datagridview1.datasource = dt;
        datagridview1.refresh();
      }
    }
    }

C# 解决datagridview控件显示大量数据拖拉卡顿问题

到此这篇关于c# 解决datagridview控件显示大量数据拖拉卡顿问题的文章就介绍到这了,更多相关c#  datagridview拖拉卡顿内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!