Hi lawal,
Refer the below code.
VB.Net
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
GridView1.DataSource = GetData()
GridView1.DataBind()
End If
End Sub
Protected Sub Print(sender As Object, e As EventArgs)
Dim tbl As New System.Web.UI.HtmlControls.HtmlTable()
Dim sw1 As New System.IO.StringWriter()
Dim hw1 As New HtmlTextWriter(sw1)
Dim td2 As New System.Web.UI.HtmlControls.HtmlTableCell()
Dim tr2 As New System.Web.UI.HtmlControls.HtmlTableRow()
tbl1.RenderControl(hw1)
Dim tbHTML As String = sw1.ToString().Replace("""", "'").Replace(System.Environment.NewLine, "")
td2.InnerHtml = tbHTML
tr2.Cells.Add(td2)
Dim dt As System.Data.DataTable = GetData()
dt.Columns.RemoveAt(0)
GridView1.HeaderRow.Cells(0).Visible = False
For i As Integer = 0 To GridView1.Rows.Count - 1
Dim row As GridViewRow = GridView1.Rows(i)
row.Cells(0).Visible = False
Next
Dim sw2 As New System.IO.StringWriter()
Dim hw2 As New HtmlTextWriter(sw2)
GridView1.RenderControl(hw2)
Dim gridHTML As String = sw2.ToString().Replace("""", "'").Replace(System.Environment.NewLine, "")
Dim tr1 As New System.Web.UI.HtmlControls.HtmlTableRow()
Dim td1 As New System.Web.UI.HtmlControls.HtmlTableCell()
td1.InnerHtml = gridHTML
tr1.Cells.Add(td1)
tbl.Rows.Add(tr1)
tbl.Rows.Add(tr2)
Dim sw3 As New System.IO.StringWriter()
Dim hw3 As New HtmlTextWriter(sw3)
tbl.RenderControl(hw3)
Dim tblHTML As String = sw3.ToString().Replace("""", "'").Replace(System.Environment.NewLine, "")
Dim sb As New System.Text.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(""")
sb.Append(tblHTML)
sb.Append(""");")
sb.Append("printWin.document.close();")
sb.Append("printWin.focus();")
sb.Append("printWin.print();")
sb.Append("printWin.close();};")
sb.Append("</script>")
ClientScript.RegisterStartupScript(Me.[GetType](), "GridPrint", sb.ToString())
GridView1.DataSource = GetData()
GridView1.DataBind()
End Sub
Public Overrides Sub VerifyRenderingInServerForm(control As Control)
' Verifies that the control is rendered
End Sub
Private Function GetData() As System.Data.DataTable
Dim dt As New System.Data.DataTable()
dt.Columns.AddRange(New System.Data.DataColumn(2) {
New System.Data.DataColumn("Id", GetType(Integer)),
New System.Data.DataColumn("Name", GetType(String)),
New System.Data.DataColumn("Country", GetType(String))})
dt.Rows.Add(1, "John Hammond", "United States")
dt.Rows.Add(2, "Mudassar Khan", "India")
dt.Rows.Add(3, "Suzanne Mathews", "France")
dt.Rows.Add(4, "Robert Schidner", "Russia")
Return dt
End Function
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
GridView1.DataSource = GetData();
GridView1.DataBind();
}
}
protected void Print(object sender, EventArgs e)
{
System.Web.UI.HtmlControls.HtmlTable tbl = new System.Web.UI.HtmlControls.HtmlTable();
System.IO.StringWriter sw1 = new System.IO.StringWriter();
HtmlTextWriter hw1 = new HtmlTextWriter(sw1);
System.Web.UI.HtmlControls.HtmlTableCell td2 = new System.Web.UI.HtmlControls.HtmlTableCell();
System.Web.UI.HtmlControls.HtmlTableRow tr2 = new System.Web.UI.HtmlControls.HtmlTableRow();
tbl1.RenderControl(hw1);
string tbHTML = sw1.ToString().Replace("\"", "'").Replace(System.Environment.NewLine, "");
td2.InnerHtml = tbHTML;
tr2.Cells.Add(td2);
System.Data.DataTable dt = GetData();
dt.Columns.RemoveAt(0);
GridView1.HeaderRow.Cells[0].Visible = false;
for (int i = 0; i < GridView1.Rows.Count; i++)
{
GridViewRow row = GridView1.Rows[i];
row.Cells[0].Visible = false;
}
System.IO.StringWriter sw2 = new System.IO.StringWriter();
HtmlTextWriter hw2 = new HtmlTextWriter(sw2);
GridView1.RenderControl(hw2);
string gridHTML = sw2.ToString().Replace("\"", "'").Replace(System.Environment.NewLine, "");
System.Web.UI.HtmlControls.HtmlTableRow tr1 = new System.Web.UI.HtmlControls.HtmlTableRow();
System.Web.UI.HtmlControls.HtmlTableCell td1 = new System.Web.UI.HtmlControls.HtmlTableCell();
td1.InnerHtml = gridHTML;
tr1.Cells.Add(td1);
tbl.Rows.Add(tr1);
tbl.Rows.Add(tr2);
System.IO.StringWriter sw3 = new System.IO.StringWriter();
HtmlTextWriter hw3 = new HtmlTextWriter(sw3);
tbl.RenderControl(hw3);
string tblHTML = sw3.ToString().Replace("\"", "'").Replace(System.Environment.NewLine, "");
System.Text.StringBuilder sb = new System.Text.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(\"");
sb.Append(tblHTML);
sb.Append("\");");
sb.Append("printWin.document.close();");
sb.Append("printWin.focus();");
sb.Append("printWin.print();");
sb.Append("printWin.close();};");
sb.Append("</script>");
ClientScript.RegisterStartupScript(this.GetType(), "GridPrint", sb.ToString());
GridView1.DataSource = GetData();
GridView1.DataBind();
}
public override void VerifyRenderingInServerForm(Control control)
{
/* Verifies that the control is rendered */
}
private System.Data.DataTable GetData()
{
System.Data.DataTable dt = new System.Data.DataTable();
dt.Columns.AddRange(new System.Data.DataColumn[3] {
new System.Data.DataColumn("Id", typeof(int)),
new System.Data.DataColumn("Name", typeof(string)),
new System.Data.DataColumn("Country",typeof(string)) });
dt.Rows.Add(1, "John Hammond", "United States");
dt.Rows.Add(2, "Mudassar Khan", "India");
dt.Rows.Add(3, "Suzanne Mathews", "France");
dt.Rows.Add(4, "Robert Schidner", "Russia");
return dt;
}
HTML
<div>
<div id="dvGrid" runat="server">
<asp:GridView ID="GridView1" HeaderStyle-BackColor="#3AC0F2" HeaderStyle-ForeColor="White"
runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="Id" HeaderText="Id" ItemStyle-Width="30" />
<asp:BoundField DataField="Name" HeaderText="Name" ItemStyle-Width="150" />
<asp:BoundField DataField="Country" HeaderText="Country" ItemStyle-Width="150" />
</Columns>
</asp:GridView>
</div>
<div>
Welcome to AspSnippets.
</div>
<table id="tbl1" runat="server">
<tr>
<td>
Sale No :
<asp:Label ID="Label1" Text="1" runat="server" />
</td>
</tr>
<tr>
<td>
Date:
<asp:Label ID="Label2" Text="19/06/2017" runat="server" />
</td>
</tr>
</table>
<br />
<asp:Button ID="Button1" Text="Print" runat="server" OnClick="Print" />
</div>