Hi nisss,
Check this sample. now take its reference.
HTML
Enter Country Name :
<asp:TextBox ID="txtCountry" runat="server" AutoPostBack="true" OnTextChanged="OnCountryChanged" />
<br />
<asp:GridView ID="gvEmployees" runat="server" AllowPaging="true" PageSize="4" OnPageIndexChanging="OnPageIndexChanging">
</asp:GridView><br />
<asp:Button ID="btnPrint" Text="Print Page" runat="server" OnClick="OnPrint" />
Namespaces
C#
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Text;
VB.Net
Imports System.Configuration
Imports System.Data
Imports System.Data.SqlClient
Imports System.IO
Imports System.Text
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.BindEmployees(txtCountry.Text.Trim());
}
}
protected void OnPrint(object sender, EventArgs e)
{
gvEmployees.AllowPaging = false;
this.BindEmployees(txtCountry.Text.Trim());
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
gvEmployees.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(\"");
sb.Append(gridHTML);
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());
gvEmployees.AllowPaging = true;
gvEmployees.DataBind();
}
protected void OnCountryChanged(object sender, EventArgs e)
{
this.BindEmployees(txtCountry.Text.Trim());
}
protected void OnPageIndexChanging(object sender, GridViewPageEventArgs e)
{
this.gvEmployees.PageIndex = e.NewPageIndex;
this.BindEmployees(txtCountry.Text.Trim());
}
public override void VerifyRenderingInServerForm(Control control)
{
/* Verifies that the control is rendered */
}
private void BindEmployees(string country)
{
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ConnectionString))
{
using (SqlCommand cmd = new SqlCommand("SELECT EmployeeID,(FIrstName+' '+LastName) AS Name,City,Country FROM Employees WHERE Country=@Country OR @Country IS NULL", con))
{
cmd.CommandType = CommandType.Text;
if (!string.IsNullOrEmpty(country))
{
cmd.Parameters.AddWithValue("@Country", country);
}
else
{
cmd.Parameters.AddWithValue("@Country", DBNull.Value);
}
using (SqlDataAdapter sda = new SqlDataAdapter())
{
sda.SelectCommand = cmd;
DataTable dt = new DataTable();
sda.Fill(dt);
this.gvEmployees.DataSource = dt;
this.gvEmployees.DataBind();
}
}
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Me.BindEmployees(txtCountry.Text.Trim())
End If
End Sub
Protected Sub OnPrint(ByVal sender As Object, ByVal e As EventArgs)
gvEmployees.AllowPaging = False
Me.BindEmployees(txtCountry.Text.Trim())
Dim sw As StringWriter = New StringWriter()
Dim hw As HtmlTextWriter = New HtmlTextWriter(sw)
gvEmployees.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(""")
sb.Append(gridHTML)
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())
gvEmployees.AllowPaging = True
gvEmployees.DataBind()
End Sub
Protected Sub OnCountryChanged(ByVal sender As Object, ByVal e As EventArgs)
Me.BindEmployees(txtCountry.Text.Trim())
End Sub
Protected Sub OnPageIndexChanging(ByVal sender As Object, ByVal e As GridViewPageEventArgs)
Me.gvEmployees.PageIndex = e.NewPageIndex
Me.BindEmployees(txtCountry.Text.Trim())
End Sub
Public Overrides Sub VerifyRenderingInServerForm(ByVal control As Control)
End Sub
Private Sub BindEmployees(ByVal country As String)
Using con As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("constr").ConnectionString)
Using cmd As SqlCommand = New SqlCommand("SELECT EmployeeID,(FIrstName+' '+LastName) AS Name,City,Country FROM Employees WHERE Country=@Country OR @Country IS NULL", con)
cmd.CommandType = CommandType.Text
If Not String.IsNullOrEmpty(country) Then
cmd.Parameters.AddWithValue("@Country", country)
Else
cmd.Parameters.AddWithValue("@Country", DBNull.Value)
End If
Using sda As SqlDataAdapter = New SqlDataAdapter()
sda.SelectCommand = cmd
Dim dt As DataTable = New DataTable()
sda.Fill(dt)
Me.gvEmployees.DataSource = dt
Me.gvEmployees.DataBind()
End Using
End Using
End Using
End Sub
Screenshot
![](https://i.imgur.com/cAo87FQ.gif)