简体   繁体   English

将分页添加到我的ASP.NET GridView

[英]Adding paging to my ASP.NET GridView

I have to display and extract 100k+ record table. 我必须显示并提取100k +记录表。

I was using GridView but its not showing data as memoryException occur. 我正在使用GridView,但由于memoryException发生而没有显示数据。

So I want to add paging system to my GridView. 所以我想向我的GridView添加分页系统。 I tried various tutorials but all covers when page loads with gridview. 我尝试了各种教程,但所有内容都涵盖了使用gridview加载页面时的情况。 But in my case GridView loads when requested with a button press. 但是在我的情况下,GridView在按按钮要求时加载。

How to bind my code for paging, so every page show 10 - 20 records? 如何绑定我的代码进行分页,以便每个页面显示10-20条记录?

here is my code-behind: 这是我的代码背后:

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();
                   }
              }
         }
    }
}

Updated my code with this is last part: 最后更新了我的代码:

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

The problem is, when I export the file to excel. 问题是,当我将文件导出到excel时。 it creates paging in excel file and that don't work like it should. 它会在excel文件中创建分页,但无法正常运行。 It show paging in the excel sheet and that clicks don't work. 它在excel工作表中显示分页,并且单击不起作用。

Paging is a very basic task 分页是一项非常基本的任务

Here is a tutorial to guide you: Paging and Sorting the GridView's Data 这是一个指导您的教程: 对GridView的数据进行分页和排序

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

The main problem however is that you are loading all the data into a DataTable. 但是,主要问题是您要将所有数据加载到DataTable中。 This will load all the data in memory. 这会将所有数据加载到内存中。 You should be using a SqlDataSource instead. 您应该改用SqlDataSource The tutorial above also shows you how to use a SqlDataSource. 上面的教程还向您展示了如何使用SqlDataSource。

Edit : 编辑

Set SqlDatasource on button click: 在按钮单击上设置SqlDatasource:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM