添加分页到我的ASP.NET GridView

时间:2013-08-02 06:45:13

标签: c# asp.net tsql gridview paging

我必须显示并提取100k +记录表。

我正在使用GridView,但它没有显示数据,因为发生了memoryException。

所以我想在我的GridView中添加分页系统。我尝试了各种教程,但是当使用gridview加载页面时我都会介绍。但在我的情况下,GridView会在按下按钮时加载。

如何绑定我的代码进行分页,所以每个页面显示10 - 20条记录吗?

这是我的代码隐藏:

protected void ExportToExcel(object sender, EventArgs e)
{
    Response.Clear();
    Response.Buffer = true;
    Response.AddHeader("content-disposition", "attachment;filename=Pfilename.xls");
    Response.Charset = "";
    Response.ContentType = "application/vnd.ms-excel";

    using (StringWriter sw = new StringWriter())
    {
        HtmlTextWriter hw = new HtmlTextWriter(sw);

        //To Export all pages
        GridView1.AllowPaging = false;


        GridView1.HeaderRow.BackColor = Color.White;
        foreach (TableCell cell in GridView1.HeaderRow.Cells)
        {
            cell.BackColor = GridView1.HeaderStyle.BackColor;
        }
        foreach (GridViewRow row in GridView1.Rows)
        {
            row.BackColor = Color.White;
            foreach (TableCell cell in row.Cells)
            {
                if (row.RowIndex % 2 == 0)
                {
                    cell.BackColor = GridView1.AlternatingRowStyle.BackColor;
                }
                else
                {
                    cell.BackColor = GridView1.RowStyle.BackColor;
                }
                cell.CssClass = "textmode";
                List<Control> controls = new List<Control>();

                //Add controls to be removed to Generic List
                foreach (Control control in cell.Controls)
                {
                    controls.Add(control);
                }

                //Loop through the controls to be removed and replace then with Literal
                foreach (Control control in controls)
                {
                    switch (control.GetType().Name)
                    {
                        case "HyperLink":
                            cell.Controls.Add(new Literal { Text = (control as HyperLink).Text });
                            break;
                        case "TextBox":
                            cell.Controls.Add(new Literal { Text = (control as TextBox).Text });
                            break;
                        case "LinkButton":
                            cell.Controls.Add(new Literal { Text = (control as LinkButton).Text });
                            break;
                        case "CheckBox":
                            cell.Controls.Add(new Literal { Text = (control as CheckBox).Text });
                            break;
                        case "RadioButton":
                            cell.Controls.Add(new Literal { Text = (control as RadioButton).Text });
                            break;
                    }
                    cell.Controls.Remove(control);
                }
            }
        }

        GridView1.RenderControl(hw);

        //style to format numbers to string
        string style = @"<style> .textmode { mso-number-format:\@; } </style>";
        Response.Write(style);
        Response.Output.Write(sw.ToString());
        Response.Flush();
        Response.End();
    }
}

protected void ViewPP_Click(object sender, EventArgs e)
{
    string strConnString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
    using (SqlConnection con = new SqlConnection(strConnString))
    {
        using (SqlCommand cmd = new SqlCommand("SELECT * FROM Table"))
        {
            using (SqlDataAdapter sda = new SqlDataAdapter())
            {
                cmd.Connection = con;
                sda.SelectCommand = cmd;

                   using (DataTable dt = new DataTable())
                   {
                        sda.Fill(dt);
                        GridView1.DataSource = dt;
                        GridView1.DataBind();
                   }
              }
         }
    }
}

更新了我的代码,这是最后一部分:

protected void OnPageIndexChanging(object sender, GridViewPageEventArgs e)
{
    GridView1.PageIndex = e.NewPageIndex;
    GridView1.DataBind();
}

问题是,当我将文件导出到excel时。它在excel文件中创建分页,并且不能正常工作。它在excel表中显示分页,并且单击不起作用。

1 个答案:

答案 0 :(得分:1)

分页是一项非常基本的任务

以下是指导您的教程:Paging and Sorting the GridView's Data

<asp:GridView ID="GridView1" Runat="server" 
     AutoGenerateColumns="False"
     AllowPaging="True" >

然而,主要问题是您正在将所有数据加载到DataTable中。这将加载内存中的所有数据。您应该使用SqlDataSource代替。上面的教程还向您展示了如何使用SqlDataSource。

修改

点击按钮设置SqlDatasource:

protected void Button_Click(object sender, EventArgs e)
{
  GridView1.DataSource = SqlDataSource1;
  GridView1.DataBind();
}