Hi anjali600,
Check this example. Now please take its reference and correct your code.
HTML
<div id="CalculateGrid" style="margin-top: 60px">
<asp:Label ID="lblDateTime" runat="server" Style="margin-left: 25%; display: none;" />
<div style="margin-left: 25%;">
<asp:GridView ID="grdCalculate" runat="server" GridLines="Horizontal" CssClass="grid"
OnRowDataBound="grdCalculate_RowDataBound">
</asp:GridView>
</div>
</div>
<button class="btns" id="print" onclick="printContent('CalculateGrid');"><i class="fa fa-print">Print</i></button>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
function printContent(el) {
var restorepage = $('body').html();
document.getElementById('<%=lblDateTime.ClientID %>').style.display = "block";
document.getElementById('<%=lblDateTime.ClientID %>').innerHTML = new Date().toLocaleString();
var printcontent = $('#' + el).clone();
$('body').empty().html(printcontent);
window.print();
$('body').html(restorepage);
}
</script>
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[3]
{
new DataColumn("Id"),
new DataColumn("Name"),
new DataColumn("Country")
});
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");
grdCalculate.DataSource = dt;
grdCalculate.DataBind();
}
}
protected void grdCalculate_RowDataBound(object sender, GridViewRowEventArgs e)
{
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Dim dt As DataTable = New DataTable()
dt.Columns.AddRange(New DataColumn(2) {
New DataColumn("Id"),
New DataColumn("Name"),
New DataColumn("Country")})
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")
grdCalculate.DataSource = dt
grdCalculate.DataBind()
End If
End Sub
Protected Sub grdCalculate_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
End Sub
Screenshot