如何提高大量数据的gridview性能? [英] How to improve gridview performance for large amount of data ?

查看:81
本文介绍了如何提高大量数据的gridview性能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨朋友们,



我正在搜索名称和id的数据,过滤的数据绑定到gridview。



我正在使用Gridview1_PageIndexChanging事件进行分页。我将过滤后的数据

存储到viewstate中,并在页面索引更改中绑定viewstate数据。



Datatable有近6000个recods。



将数据加载到gridview和页面索引更改的性能非常慢。



如何提高gridview的加载性能?



我的尝试:



grdMembers.PageIndex = e.NewPageIndex;



if(ViewState [grdview]!= null)

{



DataTable Dt =(DataTable)ViewState [grdview];

grdMembers.DataSource = Dt;

grdMembers.DataBind();

}

Hi friends,

I am searching data with name and id the filtered data is binding to gridview.

I am using Gridview1_PageIndexChanging Event for paging.I am storing filtered data
into viewstate and binding viewstate data in the page index changing.

Datatable has nearly 6000+ recods.

Performance of loading data into gridview and on page index changing is very slow.

how to improve loading performance of gridview?

What I have tried:

grdMembers.PageIndex = e.NewPageIndex;

if (ViewState["grdview"] != null)
{

DataTable Dt = (DataTable)ViewState["grdview"];
grdMembers.DataSource = Dt;
grdMembers.DataBind();
}

推荐答案

这个应该可以帮到你。

如果您使用存储过程从数据库中获取数据,您应该使用分页即发送页码和页面大小然后选择那些特定记录并返回gridview的页面索引时改变了(谷歌将提供很多例子)。此外,您应该创建所需的索引以使您的查询更好地执行。例如,如果您在查询中使用的列上没有索引,则根据需要创建索引。名称列上还应该有一个非聚集索引。



另一种替代方法可能是显示最近的记录,例如如果表中的时间戳列按此列按降序排序并返回前1000条记录。
If you are using stored procedure to fetch data from database, you should use pagination i.e. send the page number and and page size and then select those specific records and return when page index of gridview is changed (google will give lot of examples). Also, you should create required indexes to make your query perform better. For example, if you do not have indexes on the columns you are using in your query, then create index(es) as required. Name column should also have a non-clustered index on it.

Another alternate could be to display recent records e.g. if you have a timestamp column in your table sort in descending order by this column and return top 1000 records.


使用分页

Use Paging
<%@ Page language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">

  protected void PageDropDownList_SelectedIndexChanged(Object sender, EventArgs e)
  {

    // Retrieve the pager row.
    GridViewRow pagerRow = CustomersGridView.BottomPagerRow;

    // Retrieve the PageDropDownList DropDownList from the bottom pager row.
    DropDownList pageList = (DropDownList)pagerRow.Cells[0].FindControl("PageDropDownList");

    // Set the PageIndex property to display that page selected by the user.
    CustomersGridView.PageIndex = pageList.SelectedIndex;

  }

  protected void CustomersGridView_DataBound(Object sender, EventArgs e)
  {

    // Retrieve the pager row.
    GridViewRow pagerRow = CustomersGridView.BottomPagerRow;

    // Retrieve the DropDownList and Label controls from the row.
    DropDownList pageList = (DropDownList)pagerRow.Cells[0].FindControl("PageDropDownList");
    Label pageLabel = (Label)pagerRow.Cells[0].FindControl("CurrentPageLabel");

    if(pageList != null)
    {

      // Create the values for the DropDownList control based on 
      // the  total number of pages required to display the data
      // source.
      for(int i=0; i<CustomersGridView.PageCount; i++)
      {

        // Create a ListItem object to represent a page.
        int pageNumber = i + 1;
        ListItem item = new ListItem(pageNumber.ToString());         

        // If the ListItem object matches the currently selected
        // page, flag the ListItem object as being selected. Because
        // the DropDownList control is recreated each time the pager
        // row gets created, this will persist the selected item in
        // the DropDownList control.   
        if(i==CustomersGridView.PageIndex)
        {
          item.Selected = true;
        }

        // Add the ListItem object to the Items collection of the 
        // DropDownList.
        pageList.Items.Add(item);

      }

    }

    if(pageLabel != null)
    {

      // Calculate the current page number.
      int currentPage = CustomersGridView.PageIndex + 1;     

      // Update the Label control with the current page information.
      pageLabel.Text = "Page " + currentPage.ToString() +
        " of " + CustomersGridView.PageCount.ToString();

    }    

  }

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>GridView PagerTemplate Example</title>
</head>
<body>
    <form id="form1" runat="server">

      <h3>GridView PagerTemplate Example</h3>

      <asp:gridview id="CustomersGridView" 
        datasourceid="CustomersSqlDataSource"   
        autogeneratecolumns="true"
        allowpaging="true"
        ondatabound="CustomersGridView_DataBound"  
        runat="server">

        <pagerstyle forecolor="Blue"
          backcolor="LightBlue"/>

        <pagertemplate>

          <table width="100%">                    
            <tr>                        
              <td style="width:70%">

                <asp:label id="MessageLabel"
                  forecolor="Blue"
                  text="Select a page:" 
                  runat="server"/>
                <asp:dropdownlist id="PageDropDownList"
                  autopostback="true"
                  onselectedindexchanged="PageDropDownList_SelectedIndexChanged" 
                  runat="server"/>

              </td>   

              <td style="width:70%; text-align:right">

                <asp:label id="CurrentPageLabel"
                  forecolor="Blue"
                  runat="server"/>

              </td>

            </tr>                    
          </table>

        </pagertemplate> 

      </asp:gridview>

      <!-- This example uses Microsoft SQL Server and connects  -->
      <!-- to the Northwind sample database. Use an ASP.NET     -->
      <!-- expression to retrieve the connection string value   -->
      <!-- from the Web.config file.                            -->
      <asp:sqldatasource id="CustomersSqlDataSource"  
        selectcommand="Select [CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country] From [Customers]"
        connectionstring="<%


这篇关于如何提高大量数据的gridview性能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆