Friday, 24 February 2012

How to print the gridview in asp.net

create a database name log and run the following script
USE [log]
GO
/****** Object:  Table [dbo].[Student_S]    Script Date: 02/24/2012 13:11:01 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[Student_S](
 [id] [int] IDENTITY(1,1) NOT NULL,
 [Sid] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
 [Firstname] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
 [LastName] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
 [Phone] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
 [Department] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
 
 CONSTRAINT [PK_Student_S] PRIMARY KEY CLUSTERED 
(
 [Sid] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]

GO
SET ANSI_PADDING OFF
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="printgrid.aspx.cs" Inherits="printgrid" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
 
<script type="text/javascript" language="javascript">
   function CallPrint( strid )
   {
    var prtContent = document.getElementById( strid );
    var WinPrint = window.open('', '', 'left=0,top=0,width=900,height=600,toolbar=1,scrollbars=1,status=0');
    WinPrint.document.write( prtContent.innerHTML );
    WinPrint.document.close();
    WinPrint.focus();
   WinPrint.print();
  
   //WinPrint.close();
   prtContent.innerHTML=strOldOne;
   }
</script>
   <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <h2 style="color: blue; font-style: normal">
        Gridview Print in asp.net
    </h2>
    <div id="Print_Grid">
        <asp:GridView ID="GridView1" runat="server" AllowPaging="True" AutoGenerateColumns="False"
            DataKeyNames="Sid" DataSourceID="SqlDataSource1" CellPadding="4" ForeColor="#333333"
            GridLines="None">
            <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
            <RowStyle BackColor="#EFF3FB" />
            <Columns>
                <asp:BoundField DataField="id" HeaderText="id" InsertVisible="False" ReadOnly="True"
                    SortExpression="id" />
                <asp:BoundField DataField="Sid" HeaderText="Sid" ReadOnly="True" SortExpression="Sid" />
                <asp:BoundField DataField="Firstname" HeaderText="Firstname" SortExpression="Firstname" />
                <asp:BoundField DataField="LastName" HeaderText="LastName" SortExpression="LastName" />
                <asp:BoundField DataField="Date_Of_join" HeaderText="Date_Of_join" SortExpression="Date_Of_join" />
                <asp:BoundField DataField="Phone" HeaderText="Phone" SortExpression="Phone" />
            </Columns>
            <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
            <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
            <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
            <EditRowStyle BackColor="#2461BF" />
            <AlternatingRowStyle BackColor="White" />
        </asp:GridView>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:logConnectionString %>"
            SelectCommand="SELECT [id], [Sid], [Firstname], [LastName], [Date Of join] AS Date_Of_join, [Phone] FROM [Student_S]">
        </asp:SqlDataSource>
        <!-- <asp:HiddenField ID="hndGridViewPrintContent" runat="server" /> -->
    </div>
    <asp:Button Text="Print" runat="server" ID="btnPrint" OnClientClick="CallPrint('Print_Grid')"
        ForeColor="Blue" />
    </form>
</body>

</html>
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class printgrid : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
}

No comments:

Post a Comment