Hi aspvijay04,
Please take reference the below code and correct your code.
When you export GridView you should set Paging property to False and call Bind Gridview method inside Export method.
HTML
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" Font-Names="Arial"
Font-Size="11pt" AlternatingRowStyle-BackColor="#C2D69B" HeaderStyle-BackColor="green"
AllowPaging="true" OnPageIndexChanging="OnPageIndexChanging">
<Columns>
<asp:BoundField ItemStyle-Width="150px" DataField="CustomerID" HeaderText="CustomerID" />
<asp:BoundField ItemStyle-Width="150px" DataField="City" HeaderText="City" />
<asp:BoundField ItemStyle-Width="150px" DataField="PostalCode" HeaderText="PostalCode" />
</Columns>
</asp:GridView>
</div>
<br />
<asp:Button ID="btnPrint" runat="server" Text="Print" OnClick="Print" />
Namespaces
C#
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Text;
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
Response.Cache.SetCacheability(HttpCacheability.NoCache);
this.BindGrid();
}
private void BindGrid()
{
DataSet ds = new DataSet();
ds.ReadXml(Server.MapPath("~/Customers.xml"));
this.GridView1.DataSource = ds;
this.GridView1.DataBind();
}
public override void VerifyRenderingInServerForm(Control control)
{
}
protected void Print(object sender, EventArgs e)
{
this.GridView1.AllowPaging = false;
this.BindGrid();
this.GridView1.UseAccessibleHeader = true;
this.GridView1.HeaderRow.TableSection = TableRowSection.TableHeader;
this.GridView1.FooterRow.TableSection = TableRowSection.TableFooter;
this.GridView1.Attributes["style"] = "border-collapse:separate";
foreach (GridViewRow row in GridView1.Rows)
{
if (row.RowIndex % 10 == 0 && row.RowIndex != 0)
{
row.Attributes["style"] = "page-break-after:always;";
}
}
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
GridView1.RenderControl(hw);
string gridHTML = sw.ToString().Replace("\"", "'").Replace(System.Environment.NewLine, "");
StringBuilder sb = new StringBuilder();
sb.Append("<script type = 'text/javascript'>");
sb.Append("window.onload = new function(){");
sb.Append("var printWin = window.open('', '', 'left=0");
sb.Append(",top=0,width=1000,height=600,status=0');");
sb.Append("printWin.document.write(\"");
string style = "<style type = 'text/css'>thead {display:table-header-group;} tfoot{display:table-footer-group;}</style>";
sb.Append(style + gridHTML);
sb.Append("\");");
sb.Append("printWin.document.close();");
sb.Append("printWin.focus();");
sb.Append("printWin.print();");
sb.Append("printWin.close();");
sb.Append("};");
sb.Append("</script>");
ClientScript.RegisterStartupScript(this.GetType(), "GridPrint", sb.ToString());
GridView1.AllowPaging = true;
GridView1.DataBind();
}
protected void OnPageIndexChanging(object sender, GridViewPageEventArgs e)
{
this.GridView1.PageIndex = e.NewPageIndex;
this.BindGrid();
}