我必须显示并提取 100k+ 条记录表。
我正在使用 GridView,但它没有显示数据,因为发生了内存异常。
所以我想将分页系统添加到我的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 工作表中显示分页,并且单击不起作用。
分页是一项非常基本的任务
这里有一个教程来指导您:对 GridView 的数据进行分页和排序
<asp:GridView ID="GridView1" Runat="server"
AutoGenerateColumns="False"
AllowPaging="True" >
主要问题是您将所有数据加载到数据表中。这将加载内存中的所有数据。您应该使用 SqlDataSource 来代替。上面的教程还向您展示了如何使用 SqlDataSource。
编辑:
单击按钮时设置 SqlDatasource:
protected void Button_Click(object sender, EventArgs e)
{
GridView1.DataSource = SqlDataSource1;
GridView1.DataBind();
}