Hi Kid.live,
Please refer below sample.
HTML
<asp:GridView runat="server" CssClass="table table-striped table-bordered"
OnRowCommand="gvDetails_RowCommand" ID="gvDetails" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="amount" ItemStyle-Font-Bold="true" ItemStyle-Font-Size="Large" HeaderText="Amount" />
<asp:BoundField DataField="branch_num" HeaderText="Branch No" />
<asp:BoundField DataField="machineID" HeaderText="Machine ID" />
<asp:BoundField DataField="shift" HeaderText="Shifts" />
<asp:BoundField DataField="userID" HeaderText="User" />
<asp:BoundField DataField="voucher_date" HeaderText="Voucher Date" />
<asp:BoundField DataField="voucher_number" HeaderText="Voucher No" />
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="IBPrint" CommandName="Select" CommandArgument="<%# Container.DataItemIndex %>"
CssClass="btn btn-outline-dark rounded-circle" runat="server">
<i class="bi bi-printer"></i>Print
</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<PagerStyle HorizontalAlign="Center" CssClass="GridPager" />
</asp:GridView>
Namespaces
C#
using System.Data;
using System.Text;
using System.IO;
VB.Net
Imports System.Data
Imports System.Text
Imports System.IO
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();
{
dt.Columns.AddRange(new DataColumn[]
{
new DataColumn("amount", typeof(int)),
new DataColumn("branch_num", typeof(int)),
new DataColumn("machineID", typeof(int)),
new DataColumn("Shift"),
new DataColumn("userID", typeof(int)),
new DataColumn("voucher_date", typeof(DateTime) ),
new DataColumn("voucher_number", typeof(int))
});
dt.Rows.Add(100, 1, 1,"A", 4, "07/02/2022", 13);
dt.Rows.Add(200, 2, 2, "B", 5, "08/02/2022", 14);
dt.Rows.Add(300, 3, 3, "C", 6, "09/02/2022", 15);
}
this.gvDetails.DataSource = dt;
this.gvDetails.DataBind();
}
protected void gvDetails_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Select")
{
//Determine the RowIndex of the Row whose Button was clicked.
int rowIndex = Convert.ToInt32(e.CommandArgument);
//Reference the GridView Row.
GridViewRow selectedRow = gvDetails.Rows[rowIndex];
gvDetails.Attributes["style"] = "border-collapse:separate";
foreach (GridViewRow row in gvDetails.Rows)
{
if (row == selectedRow)
{
row.Visible = true;
}
else
{
row.Visible = false;
}
}
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
gvDetails.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());
gvDetails.DataBind();
}
}
public override void VerifyRenderingInServerForm(Control control)
{
/* Verifies that the control is rendered */
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
Dim dt As DataTable = New DataTable()
If True Then
dt.Columns.AddRange(New DataColumn() {New DataColumn("amount", GetType(Integer)), New DataColumn("branch_num", GetType(Integer)), New DataColumn("machineID", GetType(Integer)), New DataColumn("Shift"), New DataColumn("userID", GetType(Integer)), New DataColumn("voucher_date", GetType(DateTime)), New DataColumn("voucher_number", GetType(Integer))})
dt.Rows.Add(100, 1, 1, "A", 4, "07/02/2022", 13)
dt.Rows.Add(200, 2, 2, "B", 5, "08/02/2022", 14)
dt.Rows.Add(300, 3, 3, "C", 6, "09/02/2022", 15)
End If
Me.gvDetails.DataSource = dt
Me.gvDetails.DataBind()
End Sub
Protected Sub gvDetails_RowCommand(ByVal sender As Object, ByVal e As GridViewCommandEventArgs)
If e.CommandName = "Select" Then
Dim rowIndex As Integer = Convert.ToInt32(e.CommandArgument)
Dim selectedRow As GridViewRow = gvDetails.Rows(rowIndex)
gvDetails.Attributes("style") = "border-collapse:separate"
For Each row As GridViewRow In gvDetails.Rows
If row.Equals(selectedRow) Then
row.Visible = True
Else
row.Visible = False
End If
Next
Dim sw As StringWriter = New StringWriter()
Dim hw As HtmlTextWriter = New HtmlTextWriter(sw)
gvDetails.RenderControl(hw)
Dim gridHTML As String = sw.ToString().Replace("""", "'").Replace(System.Environment.NewLine, "")
Dim sb As StringBuilder = 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(""")
Dim style As String = "<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(Me.GetType(), "GridPrint", sb.ToString())
gvDetails.DataBind()
End If
End Sub
Public Overrides Sub VerifyRenderingInServerForm(ByVal control As Control)
End Sub
Screenshot
Gridview
PrintPage