Hi mahjoubi,
Before passing the html to the JavaScript function PrintGrid add the Label text to the html.
That will print the Label before and after GridView.
Refer below code created using the article Print functionality in ASP.Net GridView control.
HTML
<asp:Label ID="lblHeader" Text="Welcome" runat="server" Style="display: none; font-weight: bold; text-align: center" />
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" AllowPaging="true"
PageSize="10" OnPageIndexChanging="OnPageIndexChanging">
<Columns>
<asp:BoundField DataField="ContactName" HeaderText="Contact Name" ItemStyle-Width="150px" />
<asp:BoundField DataField="City" HeaderText="City" ItemStyle-Width="100px" />
<asp:BoundField DataField="Country" HeaderText="Country" ItemStyle-Width="100px" />
</Columns>
</asp:GridView>
<asp:Label ID="lblFooter" Text="ASPSnippets" runat="server" Style="display: none;" />
<hr />
<asp:Button ID="btnPrintCurrent" runat="server" Text="Print Current Page" OnClick="PrintGridView"
CommandArgument="Current" />
<asp:Button ID="btnPrintAll" runat="server" Text="Print All Pages" OnClick="PrintGridView"
CommandArgument="All" />
<script type="text/javascript">
function PrintGrid(html, css) {
var printWin = window.open('', '', 'left=0,top=0,width=400,height=300,scrollbars=1');
printWin.document.write('<style type = "text/css">' + css + '</style>');
printWin.document.write(html);
printWin.document.close();
printWin.focus();
printWin.print();
printWin.close();
};
</script>
Namespaces
C#
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.IO;
VB.Net
Imports System.Data
Imports System.Data.SqlClient
Imports System.IO
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.BindGrid();
}
}
private void BindGrid()
{
string conString = ConfigurationManager.ConnectionStrings["conString"].ConnectionString;
using (SqlConnection con = new SqlConnection(conString))
{
using (SqlDataAdapter sda = new SqlDataAdapter("SELECT * FROM Customers", con))
{
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
}
}
protected void OnPageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
this.BindGrid();
}
protected void PrintGridView(object sender, EventArgs e)
{
//Disable Paging if all Pages need to be Printed.
if ((sender as Button).CommandArgument == "All")
{
//Disable Paging.
GridView1.AllowPaging = false;
//Re-bind the GridView.
this.BindGrid();
//For Printing Header on each Page.
GridView1.UseAccessibleHeader = true;
GridView1.HeaderRow.TableSection = TableRowSection.TableHeader;
GridView1.FooterRow.TableSection = TableRowSection.TableFooter;
GridView1.Attributes["style"] = "border-collapse:separate";
foreach (GridViewRow row in GridView1.Rows)
{
if ((row.RowIndex + 1) % GridView1.PageSize == 0 && row.RowIndex != 0)
{
row.Attributes["style"] = "page-break-after:always;";
}
}
}
else
{
//Hide the Pager.
GridView1.PagerSettings.Visible = false;
this.BindGrid();
}
using (StringWriter sw = new StringWriter())
{
//Render GridView to HTML.
HtmlTextWriter hw = new HtmlTextWriter(sw);
GridView1.RenderControl(hw);
//Enable Paging.
GridView1.AllowPaging = true;
this.BindGrid();
//Remove single quotes to avoid JavaScript error.
string gridHTML = lblHeader.Text + "<hr />";
gridHTML += sw.ToString().Replace(Environment.NewLine, "");
gridHTML += "<br />" + lblFooter.Text;
string gridCSS = gridStyles.InnerText.Replace("\"", "'").Replace(Environment.NewLine, "");
//Print the GridView.
string script = "window.onload = function() { PrintGrid('" + gridHTML + "', '" + gridCSS + "'); }";
ClientScript.RegisterStartupScript(this.GetType(), "GridPrint", script, true);
}
}
public override void VerifyRenderingInServerForm(Control control)
{
/*Verifies that the control is rendered */
}
VB.Net
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
If Not IsPostBack Then
Me.BindGrid()
End If
End Sub
Private Sub BindGrid()
Dim conString As String = ConfigurationManager.ConnectionStrings("conString").ConnectionString
Using con As New SqlConnection(conString)
Using sda As New SqlDataAdapter("SELECT * FROM Customers", con)
Using dt As New DataTable()
sda.Fill(dt)
GridView1.DataSource = dt
GridView1.DataBind()
End Using
End Using
End Using
End Sub
Protected Sub OnPageIndexChanging(sender As Object, e As GridViewPageEventArgs)
GridView1.PageIndex = e.NewPageIndex
Me.BindGrid()
End Sub
Protected Sub PrintGridView(ByVal sender As Object, ByVal e As EventArgs)
'Disable Paging if all Pages need to be Printed.
If (CType(sender, Button).CommandArgument = "All") Then
'Disable Paging.
GridView1.AllowPaging = False
'Re-bind the GridView.
Me.BindGrid()
'For Printing Header on each Page.
GridView1.UseAccessibleHeader = True
GridView1.HeaderRow.TableSection = TableRowSection.TableHeader
GridView1.FooterRow.TableSection = TableRowSection.TableFooter
GridView1.Attributes("style") = "border-collapse:separate"
For Each row As GridViewRow In GridView1.Rows
If (row.RowIndex + 1) Mod GridView1.PageSize = 0 AndAlso row.RowIndex <> 0 Then
row.Attributes("style") = "page-break-after:always;"
End If
Next
Else
'Hide the Pager.
GridView1.PagerSettings.Visible = False
Me.BindGrid()
End If
Using sw As StringWriter = New StringWriter
'Render GridView to HTML.
Dim hw As HtmlTextWriter = New HtmlTextWriter(sw)
GridView1.RenderControl(hw)
'Enable Paging.
GridView1.AllowPaging = True
Me.BindGrid()
'Remove single quotes to avoid JavaScript error.
Dim gridHTML As String = lblHeader.Text + "<hr />"
gridHTML += sw.ToString().Replace(Environment.NewLine, "")
gridHTML += "<br />" + lblFooter.Text
Dim gridCSS As String = gridStyles.InnerText.Replace("""", "'").Replace(Environment.NewLine, "")
' Print the GridView.
Dim script As String = "window.onload = function() { PrintGrid('" & gridHTML & "', '" & gridCSS & "'); }"
ClientScript.RegisterStartupScript(Me.GetType(), "GridPrint", script, True)
End Using
End Sub
Public Overrides Sub VerifyRenderingInServerForm(control As Control)
' Verifies that the control is rendered
End Sub
Screenshot