1

我必须显示和提取 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 工作表中显示分页,并且单击不起作用。

4

1 回答 1

1

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

这是一个指导您的教程:分页和排序 GridView 的数据

<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();
}
于 2013-08-02T06:53:30.780 回答