C# 向我的 ASP.NET GridView 添加分页
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18010394/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Adding paging to my ASP.NET GridView
提问by John Kim
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. I tried various tutorials but all covers when page loads with gridview. But in my case GridView loads when requested with a button press.
所以我想将分页系统添加到我的 GridView。我尝试了各种教程,但是当使用 gridview 加载页面时,所有教程都涵盖了。但在我的情况下,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. it creates paging in excel file and that don't work like it should. It show paging in the excel sheet and that clicks don't work.
问题是,当我将文件导出到 excel 时。它在 excel 文件中创建分页,但不能正常工作。它在 Excel 工作表中显示分页,并且点击不起作用。
采纳答案by nunespascal
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. This will load all the data in memory. You should be using a SqlDataSourceinstead. The tutorial above also shows you how to use a SqlDataSource.
然而,主要问题是您将所有数据加载到 DataTable 中。这将加载内存中的所有数据。您应该改用SqlDataSource。上面的教程还向您展示了如何使用 SqlDataSource。
Edit:
编辑:
Set SqlDatasource on button click:
在按钮单击时设置 SqlDatasource:
protected void Button_Click(object sender, EventArgs e)
{
GridView1.DataSource = SqlDataSource1;
GridView1.DataBind();
}